Class: ZTK::Logger
- Inherits:
-
Logger
- Object
- Logger
- ZTK::Logger
- 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.
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)
-
- (Object) loggers
Returns the value of attribute loggers.
Instance Method Summary (collapse)
-
- (Logger) initialize(*args)
constructor
A new instance of Logger.
-
- (Object) inspect
Generates a human-readable string about the logger.
- - (Object) level=(value)
-
- (Object) shift(severity, shift = 0, &block)
Specialized logging.
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 |