Class: ComposableAgents::Logger::Tagged

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

Overview

Proxy around any Ruby-like logger that prefixes fields to the logged messages. This is a standard ::Logger that inherits all the severity methods (debug, info, warn, error, fatal, unknown), but overrides the add/log methods to delegate the actual logging to the wrapped logger, with the given fields prefixed to the message. Messages can be given as a String or a Proc returning the message (for lazy evaluation).

Constant Summary collapse

SEVERITY_METHODS =

Mapping of severity values to the corresponding severity methods on the wrapped logger

{
  ::Logger::DEBUG => :debug,
  ::Logger::INFO => :info,
  ::Logger::WARN => :warn,
  ::Logger::ERROR => :error,
  ::Logger::FATAL => :fatal,
  ::Logger::UNKNOWN => :unknown
}.freeze

Public API collapse

Constructor Details

#initialize(logger, fields) ⇒ Tagged

Constructor

Parameters:

  • Any Ruby-like logger responding to the standard severity methods

  • Fields to be prefixed to each logged message



26
27
28
29
30
31
32
# File 'lib/composable_agents/logger/tagged.rb', line 26

def initialize(logger, fields)
  @logger = logger
  @fields = fields
  # This proxy does not log on its own: the wrapped logger does. Its level is kept in sync so that
  #   the inherited level predicates (debug?, info?...) reflect the wrapped logger's level.
  super(nil, level: logger.respond_to?(:level) ? logger.level : ::Logger::DEBUG)
end

Instance Method Details

#add(severity, message = nil, progname = nil) { ... } ⇒ Boolean

Log a message with the given severity, delegating to the wrapped logger with the fields prefixed. This is the implementation used by all severity methods, that all call #add in ::Logger.

Parameters:

  • Severity of the message

  • (defaults to: nil)

    Message string, Proc returning the message for lazy evaluation, or nil if the message is given by the progname or a block

  • (defaults to: nil)

    Program name to be logged if the message is nil

Yields:

  • The optional code returning the message to log

Yield Returns:

  • (String)

    The message to log

Returns:

  • True, as ::Logger#add does



44
45
46
# File 'lib/composable_agents/logger/tagged.rb', line 44

def add(severity, message = nil, progname = nil, &)
  log_message(severity, message, progname, &)
end

#log(severity, message = nil, progname = nil) { ... } ⇒ Boolean

Log a message with the given severity. Defined because ::Logger defines #log as an alias of #add, and aliases are bound at definition time:

without redefining it here, #log would not use this class' #add override.

Parameters:

  • Severity of the message

  • (defaults to: nil)

    Message string, Proc returning the message for lazy evaluation, or nil if the message is given by the progname or a block

  • (defaults to: nil)

    Program name to be logged if the message is nil

Yields:

  • The optional code returning the message to log

Yield Returns:

  • (String)

    The message to log

Returns:

  • True, as ::Logger#add does



59
60
61
# File 'lib/composable_agents/logger/tagged.rb', line 59

def log(severity, message = nil, progname = nil, &)
  add(severity, message, progname, &)
end