A simple class that allows logging at various levels.
configures the logger class, if the config has not yet been loaded we default to the console logging class and do not set @configured so that future calls to the log method will keep attempting to configure the logger till we eventually get a logging preference from the config module
# File lib/mcollective/log.rb, line 72 def configure(logger=nil) unless logger logger_type = "console" config = Config.instance if config.configured logger_type = config.logger_type @configured = true end require "mcollective/logger/#{logger_type.downcase}_logger" logger_class = MCollective::Logger.const_get("#{logger_type.capitalize}_logger") set_logger(logger_class.new) else set_logger(logger) @configured = true end @logger.start rescue Exception => e @configured = false STDERR.puts "Could not start logger: #{e.class} #{e}" end
increments the active log level
# File lib/mcollective/log.rb, line 43 def cycle_level @logger.cycle_level if @configured end
Logs at debug level
# File lib/mcollective/log.rb, line 23 def debug(msg) log(:debug, msg) end
Logs at error level
# File lib/mcollective/log.rb, line 33 def error(msg) log(:error, msg) end
this method is here to facilitate testing and from
# File lib/mcollective/log.rb, line 106 def execution_stack caller end
Logs at fatal level
# File lib/mcollective/log.rb, line 28 def fatal(msg) log(:fatal, msg) end
figures out the filename that called us
# File lib/mcollective/log.rb, line 100 def from path, line, method = execution_stack[3].split(/:(\d+)/) "%s:%s%s" % [File.basename(path), line, method] end
Logs at info level
# File lib/mcollective/log.rb, line 13 def info(msg) log(:info, msg) end
handle old code that relied on this class being a singleton
# File lib/mcollective/log.rb, line 38 def instance self end
logs a message at a certain level
# File lib/mcollective/log.rb, line 48 def log(level, msg) configure unless @configured raise "Unknown log level" unless [:error, :fatal, :debug, :warn, :info].include?(level) if @logger @logger.log(level, from, msg) else t = Time.new.strftime("%H:%M:%S") STDERR.puts "#{t}: #{level}: #{from}: #{msg}" end end
Obtain the class name of the currently configured logger
# File lib/mcollective/log.rb, line 8 def logger @logger.class end
Generated with the Darkfish Rdoc Generator 2.