Class: Hanami::Logger

Inherits:
Logger
  • Object
show all
Defined in:
gems/gems/hanami-utils-1.3.3/lib/hanami/logger.rb,
gems/gems/hanami-utils-1.3.3/lib/hanami/logger/filter.rb,
gems/gems/hanami-utils-1.3.3/lib/hanami/logger/colorizer.rb,
gems/gems/hanami-utils-1.3.3/lib/hanami/logger/formatter.rb,
gems/gems/hanami-utils-1.3.8/lib/hanami/logger/filter.rb,
gems/gems/hanami-utils-1.3.8/lib/hanami/logger/colorizer.rb,
gems/gems/hanami-utils-1.3.8/lib/hanami/logger/formatter.rb,
gems/gems/hanami-utils-1.3.8/lib/hanami/logger.rb

Overview

Hanami logger

Implementation with the same interface of Ruby std lib Logger. It uses STDOUT, STDERR, file name or open file as output stream.

When a Hanami application is initialized, it creates a logger for that specific application. For instance for a Bookshelf::Application a Bookshelf::Logger will be available.

This is useful for auto-tagging the output. Eg (app=Booshelf).

When used standalone (eg. Hanami::Logger.info), it tags lines with app=Shared.

The available severity levels are the same of Logger:

  • DEBUG

  • INFO

  • WARN

  • ERROR

  • FATAL

  • UNKNOWN

Those levels are available both as class and instance methods.

Also Hanami::Logger supports different formatters. Now available only two:

  • Formatter (default)

  • JSONFormatter

And if you want to use custom formatter you need create new class inherited from Formatter class and define _format private method like this:

class CustomFormatter < Formatter
  private
  def _format(hash)
    # ...
  end
end

Examples:

Basic usage

require 'hanami'

module Bookshelf
  class Application < Hanami::Application
  end
end

# Initialize the application with the following code:
Bookshelf::Application.load!
# or
Bookshelf::Application.new

Bookshelf::Logger.new.info('Hello')
# => app=Bookshelf severity=INFO time=1988-09-01 00:00:00 UTC message=Hello

Standalone usage

require 'hanami/logger'

Hanami::Logger.new.info('Hello')
# => app=Hanami severity=INFO time=2016-05-27 10:14:42 UTC message=Hello

Custom tagging

require 'hanami/logger'

Hanami::Logger.new('FOO').info('Hello')
# => app=FOO severity=INFO time=2016-05-27 10:14:42 UTC message=Hello

Write to file

require 'hanami/logger'

Hanami::Logger.new(stream: 'logfile.log').info('Hello')
# in logfile.log
# => app=FOO severity=INFO time=2016-05-27 10:14:42 UTC message=Hello

Use JSON formatter

require 'hanami/logger'

Hanami::Logger.new(formatter: Hanami::Logger::JSONFormatter).info('Hello')
# => "{\"app\":\"Hanami\",\"severity\":\"INFO\",\"time\":\"1988-09-01 00:00:00 UTC\",\"message\":\"Hello\"}"

Disable colorization

require 'hanami/logger'

Hanami::Logger.new(colorizer: false)

Use custom colors

require 'hanami/logger'

Hanami::Logger.new(colorizer: Hanami::Logger::Colorizer.new(colors: { app: :red }))

Use custom colorizer

require "hanami/logger"
require "paint" # gem install paint

class LogColorizer < Hanami::Logger::Colorizer
  def initialize(colors: { app: [:red, :bright], severity: [:red, :blue], datetime: [:italic, :yellow] })
    super
  end

  private

  def colorize(message, color:)
    Paint[message, *color]
  end
end

Hanami::Logger.new(colorizer: LogColorizer.new)

See Also:

Since:

  • 0.5.0