Class: Hanami::Logger
- Inherits:
-
Logger
- Object
- Logger
- Hanami::Logger
- Defined in:
- lib/hanami/logger.rb,
lib/hanami/logger/filter.rb,
lib/hanami/logger/colorizer.rb,
lib/hanami/logger/formatter.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
Defined Under Namespace
Classes: Colorizer, Filter, Formatter, JSONFormatter, NullColorizer
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"
- 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.
Class Method Summary collapse
- .level(level) ⇒ Object private
Instance Method Summary collapse
-
#close ⇒ Object
Closes the logging stream if this stream isn’t an STDOUT.
-
#initialize(application_name = nil, *args, stream: $stdout, level: DEBUG, formatter: nil, filter: [], colorizer: nil) ⇒ Logger
constructor
Initialize a logger.
- #level=(value) ⇒ Object private
Constructor Details
#initialize(application_name = nil, *args, stream: $stdout, level: DEBUG, formatter: nil, filter: [], colorizer: nil) ⇒ Logger
Initialize a logger
rubocop:disable Lint/SuppressedException rubocop:disable Metrics/ParameterLists
284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/hanami/logger.rb', line 284 def initialize(application_name = nil, *args, stream: $stdout, level: DEBUG, formatter: nil, filter: [], colorizer: nil) # rubocop:disable Layout/LineLength begin Utils::Files.mkdir_p(stream) rescue TypeError end super(stream, *args) @level = _level(level) @stream = stream @application_name = application_name @formatter = Formatter.fabricate(formatter, self.application_name, filter, lookup_colorizer(colorizer)) end |
Instance Attribute Details
#application_name ⇒ String
Returns the current application name, this is used for tagging purposes
306 307 308 |
# File 'lib/hanami/logger.rb', line 306 def application_name @application_name || _application_name_from_namespace || _default_application_name end |
Class Method Details
.level(level) ⇒ 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.
145 146 147 148 149 150 151 152 |
# File 'lib/hanami/logger.rb', line 145 def self.level(level) case level when DEBUG..UNKNOWN level else LEVELS.fetch(level.to_s.downcase, DEBUG) end end |
Instance Method Details
#close ⇒ Object
Closes the logging stream if this stream isn’t an STDOUT
319 320 321 |
# File 'lib/hanami/logger.rb', line 319 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.
312 313 314 |
# File 'lib/hanami/logger.rb', line 312 def level=(value) super _level(value) end |