Class: Eymiha::EasyLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/eymiha/util/easylog.rb

Overview

Primarily, EasyLogger provides a convienient interface to the Logger at the class level. The class contains a “default” instance of Logger to which the class methods send their log messages. By convention, the first instance created is made the default, however this can be changed. Of course, instances may be used directly for logging if desired. Instances format their log entries using an EasyLogFormatter.

Five class methods are built that have the same names as the instance methods: debug, info, warn, error and fatal. Their arguments are different, however, being the message to be logged and optionally, one or more Loggers to log the message. If no Loggers are passed, the default is used, and if that is nil,no logging takes place.

Two other convenience class methods for each of the five Logger severities are also provided for checking and adjusting the threshold levels of the default logger. The debug?, info?, warn?, error? and fatal? methods return true if a message at that level will be reported by the default logger. Respectively, the level_debug, level_info, level_warn, level_error and level_fatal methods will set the threshold of the default logger to pass only messages at or above the selected severity.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logdev, shift_age = 'weekly', shift_size = 1048576, subsecond_precision = nil) ⇒ EasyLogger

Creates a new EasyLogger instance. Logging is recorded through the given logdev, either an IO stream or to a file when logdev is a String containing its filename. Shift age is either the number of log files to keep, or the frequency of rotation. Shift size is the maximum logfile size, applicable when shift age is a number. Subsecond precision is the number of decimal places that express fractional seconds. If this is the first instance being created, it is remembered and used by the logging methods at the class level as the default logger.



72
73
74
75
76
77
78
# File 'lib/eymiha/util/easylog.rb', line 72

def initialize(logdev, shift_age = 'weekly', shift_size = 1048576,
               subsecond_precision = nil)
  super(logdev, shift_age, shift_size)
  @formatter = EasyLogFormatter.new(subsecond_precision)
  @classify = true
  @@default_logger = self unless @@default_logger
end

Instance Attribute Details

#classifyObject

Returns the value of attribute classify.



62
63
64
# File 'lib/eymiha/util/easylog.rb', line 62

def classify
  @classify
end

Class Method Details

.change_logger(logger) ⇒ Object

Changes the default logger to the given logger.



81
82
83
# File 'lib/eymiha/util/easylog.rb', line 81

def self.change_logger(logger)
  old, @@default_logger = @@default_logger, logger
end

.finish_loggingObject

Sets the default logger to nil, indicating that further logging by the class through the default logger mechanism is disabled.



87
88
89
# File 'lib/eymiha/util/easylog.rb', line 87

def self.finish_logging
  change_logger nil
end

.levelObject

Returns the threshold of the default logger if it exists, otherwise nil.



92
93
94
# File 'lib/eymiha/util/easylog.rb', line 92

def self.level
  @@default_logger ? @@default_logger.level : nil
end