Class: Whatsapp::WLogger
- Inherits:
-
Object
- Object
- Whatsapp::WLogger
- Defined in:
- lib/whatsapp/logger.rb
Overview
Structured logger with key-value context support.
Wraps Ruby's standard +Logger+ and adds structured context formatting.
Constant Summary collapse
- LEVELS =
Available log levels.
%i[debug info warn error fatal].freeze
Instance Attribute Summary collapse
-
#backend ⇒ Logger
readonly
The underlying Ruby Logger instance.
Instance Method Summary collapse
-
#initialize(output = $stdout, level: :info) ⇒ WLogger
constructor
A new instance of WLogger.
-
#level=(lvl) ⇒ void
Set the log level.
Constructor Details
#initialize(output = $stdout, level: :info) ⇒ WLogger
Returns a new instance of WLogger.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/whatsapp/logger.rb', line 22 def initialize(output = $stdout, level: :info) @external = false @backend = if output.nil? ::Logger.new(File::NULL) elsif output.is_a?(::Logger) output elsif output.respond_to?(:info) && output.respond_to?(:debug) @external = true output else ::Logger.new(output) end unless @external @backend.level = LEVELS.index(level) || ::Logger::INFO @backend.formatter = method(:format_message) end end |
Instance Attribute Details
#backend ⇒ Logger (readonly)
Returns the underlying Ruby Logger instance.
18 19 20 |
# File 'lib/whatsapp/logger.rb', line 18 def backend @backend end |
Instance Method Details
#level=(lvl) ⇒ void
This method returns an undefined value.
Set the log level.
56 57 58 |
# File 'lib/whatsapp/logger.rb', line 56 def level=(lvl) @backend.level = LEVELS.index(lvl) || lvl unless @external end |