Class: ComposableAgents::Logger

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

Overview

Logger to be used by agents. This is a standard Ruby Logger (inheriting from ::Logger) that formats messages with a UTC timestamp and severity. It also supports prefixing fields to messages (like the agent's full name) using the #tagged method, which returns a proxy compatible with any Ruby-like logger: fields are then prepended to the logged messages themselves, and silently ignored by loggers not supporting them.

Defined Under Namespace

Classes: Tagged

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logdev = $stdout, *args, **kwargs) ⇒ Logger

Constructor. Unless overridden by the caller, the default level follows the COMPOSABLE_AGENTS_DEBUG environment variable (DEBUG when it is set to 1, INFO otherwise), and the default formatter outputs messages as [UTC timestamp] [SEVERITY] message.

Parameters:

  • logdev (IO, String, #write, nil) (defaults to: $stdout)

    Log device to be used, defaulting to standard output

  • args (Array<Object>)

    Positional arguments to be given to ::Logger (shifting parameters...)

  • kwargs (Hash{Symbol => Object})

    Optional parameters to be given to ::Logger (level, formatter...)



29
30
31
32
33
34
35
36
37
# File 'lib/composable_agents/logger.rb', line 29

def initialize(logdev = $stdout, *args, **kwargs)
  super
  self.level = ENV['COMPOSABLE_AGENTS_DEBUG'] == '1' ? DEBUG : INFO unless kwargs.key?(:level)
  return if kwargs.key?(:formatter)

  self.formatter = proc do |severity, _datetime, _progname, message|
    "[#{Time.now.utc.strftime('%F %T')}] [#{severity}] #{message}\n"
  end
end

Class Method Details

.instanceLogger

Get the default singleton logger instance

Returns:

  • (Logger)

    The default logger instance



16
17
18
# File 'lib/composable_agents/logger.rb', line 16

def instance
  @instance ||= new
end

Instance Method Details

#tagged(*fields) ⇒ Tagged

Get a logger proxy that prefixes the given fields to logged messages. The returned proxy is compatible with any Ruby-like logger.

Parameters:

  • fields (Array<String>)

    Fields to be prefixed to each logged message

Returns:

  • (Tagged)

    The fields-prefixing logger proxy



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

def tagged(*fields)
  Tagged.new(self, fields)
end