Class: Lotus::Logger

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

Overview

Lotus logger

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

When a Lotus 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. ‘Lotus::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 'lotus'

module Bookshelf
  class Application < Lotus::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 'lotus'

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

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

Custom tagging

require 'lotus'

Lotus::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

'Lotus'.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/lotus/logger.rb', line 109

def initialize(application_name = nil)
  super(STDOUT)

  @application_name = application_name
  @formatter        = Lotus::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/lotus/logger.rb', line 121

def application_name
  @application_name || _application_name_from_namespace || _default_application_name
end