lib/eymiha/util/easylog.rb in eymiha_util-1.0.1 vs lib/eymiha/util/easylog.rb in eymiha_util-1.0.2

- old
+ new

@@ -27,11 +27,13 @@ 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)] + (msg == "") ? + "\n" : + (@AppLogFormat % [format_datetime(time), severity, msg2str(msg)]) end end # Primarily, EasyLogger provides a convienient interface to the Logger at the @@ -39,15 +41,23 @@ # 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 + # 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. class EasyLogger < Logger class_attr_reader :default_logger attr_accessor :classify @@ -65,30 +75,43 @@ @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 + # Changes the default logger to the given logger. + def self.change_logger(logger) + old, @@default_logger = @@default_logger, logger end - # Sets the default logger to nil, indicating that further class level - # logging through the default logger mechanism is disabled. + # Sets the default logger to nil, indicating that further logging by the + # class through the default logger mechanism is disabled. def self.finish_logging change_logger nil end + # Returns the threshold of the default logger if it exists, otherwise nil. + def self.level + @@default_logger ? @@default_logger.level : nil + end + private def self.build_loggers (0..4).collect { |i| Logger::SEV_LABEL[i].downcase }.each do |severity| EasyLogger.class_eval <<EOF -def self.#{severity}(message,*loggers) +def self.#{severity}(message="",*loggers) loggers = [@@default_logger] if loggers.length == 0 loggers.compact.each { |logger| logger.#{severity}(message) } end + +def self.#{severity}? + @@default_logger && @@default_logger.#{severity}? +end + +def self.level_#{severity} + @@default_logger.level=Logger::#{severity.upcase} if @@default_logger +end EOF end end self.build_loggers @@ -120,17 +143,25 @@ # The module builds five methods for EasyLog logging corresponding to the # five Logger severities: log_debug, log_info, log_warn, log_error and # log_fatal. Their arguments are a message to be logged, and optionally # one or more EasyLoggers that will be the targets of the message. # - # Note that the classify attribute of an EasyLogger stores the decision - # about adding the class name to the end of the message or not; by default - # it is true and class names are added. + # Note the classify attribute of an EasyLogger stores the decision about + # adding the class name to the end of the message or not; by default it is + # true and class names are added. # # easylogger.classify = false # # will turn this off. + # + # Two other convenience methods for each of the five Logger severities are + # also provided for checking and adjusting EasyLog threshold levels. The + # log_debug?, log_info?, log_warn?, log_error? and log_fatal? methods return + # true if a message at that level will be reported. Respectively, the + # log_level_debug, log_level_info, log_level_warn, log_level_error and + # log_level_fatal methods will set the threshold to pass only messages at or + # above the selected severity. module EasyLog # Creates a new EasyLogger instance with the given arguments, and uses it # for all subsequent EasyLog logging. The arguments are the same as used in # EasyLogger creation. @@ -153,24 +184,38 @@ # Turns off EasyLog logging until restarted. def finish_logging EasyLogger.finish_logging end + # Returns the threshold of the EasyLog, or nil if no Easylogger is set. + def log_level + EasyLogger.level + end + private def classify_log_message(message,easylogger) - ((easylogger != nil) ? easylogger.classify : false) ? + ((easylogger != nil) ? + (easylogger.classify and message != "" and message != " ") : false) ? "#{message} (#{class_name})" : message end def self.build_loggers (0..4).collect { |i| Logger::SEV_LABEL[i].downcase }.each do |severity| EasyLog.module_eval <<EOF -def log_#{severity}(message,*easyloggers) +def log_#{severity}(message="",*easyloggers) easyloggers = [easylogger] if easyloggers.size == 0 - easyloggers.flatten.each { |easylogger| + easyloggers.compact.each { |easylogger| EasyLogger.#{severity}(classify_log_message(message,easylogger),easylogger) } +end + +def log_#{severity}? + EasyLogger.#{severity}? +end + +def log_level_#{severity} + EasyLogger.level_#{severity} end EOF end end