Class: Hanami::Logger
- Inherits:
-
Logger
- Object
- Logger
- Hanami::Logger
- Defined in:
- lib/hanami/logger.rb
Overview
Hanami logger
Implement 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 stand alone (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 support 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
Defined Under Namespace
Classes: Formatter, JSONFormatter
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.
'hanami'.freeze
- LEVELS =
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.
Hash[ 'debug' => DEBUG, 'info' => INFO, 'warn' => WARN, 'error' => ERROR, 'fatal' => FATAL, 'unknown' => UNKNOWN ].freeze
Instance Attribute Summary collapse
-
#application_name ⇒ String
Returns the current application name, this is used for tagging purposes.
Instance Method Summary collapse
-
#close ⇒ Object
Close the logging stream if this stream isn’t an STDOUT.
-
#initialize(application_name = nil, *args, stream: $stdout, level: DEBUG, formatter: nil) ⇒ Logger
constructor
Initialize a logger.
- #level=(value) ⇒ Object private
Constructor Details
#initialize(application_name = nil, *args, stream: $stdout, level: DEBUG, formatter: nil) ⇒ Logger
Initialize a logger
378 379 380 381 382 383 384 385 |
# File 'lib/hanami/logger.rb', line 378 def initialize(application_name = nil, *args, stream: $stdout, level: DEBUG, formatter: nil) super(stream, *args) @level = _level(level) @stream = stream @application_name = application_name @formatter = Formatter.fabricate(formatter, self.application_name) end |
Instance Attribute Details
#application_name ⇒ String
Returns the current application name, this is used for tagging purposes
392 393 394 |
# File 'lib/hanami/logger.rb', line 392 def application_name @application_name || _application_name_from_namespace || _default_application_name end |
Instance Method Details
#close ⇒ Object
Close the logging stream if this stream isn’t an STDOUT
405 406 407 |
# File 'lib/hanami/logger.rb', line 405 def close super unless [STDOUT, $stdout].include?(@stream) end |
#level=(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
398 399 400 |
# File 'lib/hanami/logger.rb', line 398 def level=(value) super _level(value) end |