Class: ZTK::Logger

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

Overview

Standard Logging Class

Allows chaining standard Ruby loggers as well as adding extra spice to your log messages. This includes uSec timestamping, PIDs and caller tree details.

This class accepts the same initialize arguments as the Ruby logger class. You can chain multiple loggers together, for example to get an effect of logging to STDOUT and a file simultaneously without having to modify your existing logging statements.

One can override the logging level on the command line with programs that use this library by defining the LOG_LEVEL environment variable to the desired logging level.

Examples:

Override the logging level at runtime


LOG_LEVEL=DEBUG bin/a_ruby_script.rb

Typical usage


$logger = ZTK::Logger.new("/dev/null")

$logger.debug { "This is a debug message!" }
$logger.info { "This is a info message!" }
$logger.warn { "This is a warn message!" }
$logger.error { "This is a error message!" }
$logger.fatal { "This is a fatal message!" }

Simple logger chaining


logger = ZTK::Logger.new
logger.loggers << ::Logger.new(STDOUT)
logger.loggers << ::Logger.new('test.log')

logger.debug { "This will be written to STDOUT as well as test.log!" }

Simple logger chaining


logger = ZTK::Logger.new(STDOUT)
logger.loggers << ::Logger.new('test.log')

logger.debug { "This will be written to STDOUT as well as test.log!" }

Author:

Defined Under Namespace

Classes: LogDevice

Constant Summary

SEVERITIES =

Log Levels

Severity.constants.inject([]) {|arr,c| arr[Severity.const_get(c)] = c; arr}

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Logger) initialize(*args)

Returns a new instance of Logger



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ztk/logger.rb', line 82

def initialize(*args)
  super(::StringIO.new)

  @loggers = Array.new
  if args.count > 0
    @loggers << ::Logger.new(*args)
  end

  @logdev = LogDevice.new(self)

  set_log_level
end

Instance Attribute Details

- (Object) loggers

Returns the value of attribute loggers



80
81
82
# File 'lib/ztk/logger.rb', line 80

def loggers
  @loggers
end

Instance Method Details

- (Object) inspect

Generates a human-readable string about the logger.



96
97
98
99
# File 'lib/ztk/logger.rb', line 96

def inspect
  loggers_inspect = @loggers.collect{|logger| logger.instance_variable_get(:@logdev).instance_variable_get(:@dev).inspect }.join(', ')
  "#<#{self.class} loggers=[#{loggers_inspect}]>"
end

- (Object) level=(value)



117
118
119
120
121
122
123
# File 'lib/ztk/logger.rb', line 117

def level=(value)
  @level = value

  @loggers.each { |logger| logger.level = @level }

  value
end

- (Object) shift(severity, shift = 0, &block)

Specialized logging. Logs messages in the same format, except has the option to shift the caller_at position to exposed the proper calling method.

Very useful in situations of class inheritence, for example, where you might have logging statements in a base class, which are inherited by another class. When calling the base class method via the inherited class the log messages will indicate the base class as the caller. While this is technically true it is not always what we want to see in the logs because it is ambigious and does not show us where the call truly originated from.



112
113
114
115
# File 'lib/ztk/logger.rb', line 112

def shift(severity, shift=0, &block)
  severity = ZTK::Logger.const_get(severity.to_s.upcase)
  add(severity, nil, nil, shift, &block)
end