require 'eymiha' require 'logger' module Eymiha # EasyLogFormatter instances are used to format logging messages generated by # the EasyLogger class. They consist of a date-time stamp, the severity and # a log message. class EasyLogFormatter < Logger::Formatter # Creates an instance, using the specified subsecond precision to format # fractional seconds. If nil, the calculated subsecond precision of the # Time class is used. def initialize(subsecond_precision = nil) super() @AppLogFormat = "%s %-5s %s\n" self.datetime_format = "%m/%d/%y %H:%M:%S." @usecs = subsecond_precision || Time.subsecond_precision end # Returns a String containing a date-time stamp for the given time in # month/day/year hour:minute:second.microsecond format with the microsecond # part having the precision specified when the instance was created. def format_datetime(time) time.strftime(datetime_format) << ("%06d" % time.usec)[0,@usecs] end # Returns a String containing a formatted log entry consisting of the given # date-time stamp, severity and log message. def call(severity, time, progname, msg) @AppLogFormat % [format_datetime(time), severity, msg2str(msg)] end end # 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-level 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. class EasyLogger < Logger class_attr_reader :default_logger attr_accessor :classify # 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. 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 # Changes the default logger to the new logger. def self.change_logger(new) old, @@default_logger = @@default_logger, new end # Sets the default logger to nil, indicating that further class level # logging through the default logger mechanism is disabled. def self.finish_logging change_logger nil end private def self.build_loggers (0..4).collect { |i| Logger::SEV_LABEL[i].downcase }.each do |severity| EasyLogger.class_eval <