lib/win32/autogui/logging.rb in win32-autogui-0.3.0 vs lib/win32/autogui/logging.rb in win32-autogui-0.4.0

- old
+ new

@@ -16,25 +16,39 @@ def logfile @filename end def logfile=(fn) - FileOutputter.new(:logfile, :filename => fn, :trunc => true) - Outputter[:logfile].formatter = Log4r::PatternFormatter.new(:pattern => "[%5l %d] %M [%t]") - add(:logfile) + if fn == nil + puts "removing log to file" + remove(:logfile) if @filename + else + FileOutputter.new(:logfile, :filename => fn, :trunc => true) + Outputter[:logfile].formatter = Log4r::PatternFormatter.new(:pattern => "[%5l %d] %M [%t]") + add(:logfile) + end + @filename = fn end end end module Autogui - STANDARD_LOGGER = 'standard' - # wrapper for Log4r gem module Logging + # Redefine logging levels so that they can be accessed before + # the logger is initialized at the expense of flexibility. + DEBUG = 1 + INFO = 2 + WARN = 3 + ERROR = 4 + FATAL = 5 + + STANDARD_LOGGER = 'standard' + # Logging mixin allows simple logging setup # to STDOUT and optionally, to one filename. Logger is a wrapper # for Log4r::Logger it accepts any methods that # Log4r::Logger accepts in addition to the "logfile" filename. # @@ -43,11 +57,11 @@ # include Autogui::Logging # # logger.filename = 'log/autogui.log' # logger.warn "warning message goes to 'log/autogui.log'" # - # logger.level = Log4r::DEBUG + # logger.level = Autogui::Logging::DEBUG # logger.debug "this message goes to 'log/autogui.log'" # def logger init_logger if Log4r::Logger[STANDARD_LOGGER].nil? Log4r::Logger[STANDARD_LOGGER] @@ -57,10 +71,20 @@ protected # Initialize the logger, defaults to log4r::Warn def init_logger log = Log4r::Logger.new(STANDARD_LOGGER) - Log4r::Logger[STANDARD_LOGGER].level = Log4r::WARN + + # sanity checks since we defined log4r's dynamic levels statically + unless (Log4r::DEBUG == DEBUG) && + (Log4r::INFO == INFO) && + (Log4r::WARN == WARN) && + (Log4r::ERROR == ERROR) && + (Log4r::FATAL == FATAL) + raise "Logger levels do not match Log4r levels, levels may have been customized" + end + + Log4r::Logger[STANDARD_LOGGER].level = WARN Log4r::Logger[STANDARD_LOGGER].trace = true Log4r::StderrOutputter.new :console Log4r::Outputter[:console].formatter = Log4r::PatternFormatter.new(:pattern => "[%l] %m") log.add(:console)