Class: Hanami::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/hanami/logger.rb

Overview

Hanami logger

Implement with the same interface of Ruby std lib Logger. It uses STDOUT as output device.

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 ([Booshelf]).

When used stand alone (eg. Hanami::Logger.info), it tags lines with [Shared].

The available severity levels are the same of Logger:

* debug
* error
* fatal
* info
* unknown
* warn

Those levels are available both as class and instance methods.

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.info('Hello')
# => I, [2015-01-10T21:55:12.727259 #80487]  INFO -- [Bookshelf] : Hello

Bookshelf::Logger.new.info('Hello')
# => I, [2015-01-10T21:55:12.727259 #80487]  INFO -- [Bookshelf] : Hello

Standalone usage

require 'hanami'

Hanami::Logger.info('Hello')
# => I, [2015-01-10T21:55:12.727259 #80487]  INFO -- [Hanami] : Hello

Hanami::Logger.new.info('Hello')
# => I, [2015-01-10T21:55:12.727259 #80487]  INFO -- [Hanami] : Hello

Custom tagging

require 'hanami'

Hanami::Logger.new('FOO').info('Hello')
# => I, [2015-01-10T21:55:12.727259 #80487]  INFO -- [FOO] : Hello

See Also:

Since:

  • 0.5.0

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

DEFAULT_APPLICATION_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default application name. This is used as a fallback for tagging purposes.

Since:

  • 0.5.0

'Hanami'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application_name = nil) ⇒ Logger

Initialize a logger

Parameters:

  • application_name (String) (defaults to: nil)

    an optional application name used for tagging purposes

Since:

  • 0.5.0



109
110
111
112
113
114
# File 'lib/hanami/logger.rb', line 109

def initialize(application_name = nil)
  super(STDOUT)

  @application_name = application_name
  @formatter        = Hanami::Logger::Formatter.new.tap { |f| f.application_name = self.application_name }
end

Instance Attribute Details

#application_nameString

Returns the current application name, this is used for tagging purposes

Returns:

  • (String)

    the application name

Since:

  • 0.5.0



121
122
123
# File 'lib/hanami/logger.rb', line 121

def application_name
  @application_name || _application_name_from_namespace || _default_application_name
end