class MCollective::Log
A simple class that allows logging at various levels.
Public Class Methods
configure(logger=nil)
click to toggle source
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 79 def configure(logger=nil) 80 unless logger 81 logger_type = "console" 82 83 config = Config.instance 84 85 if config.configured 86 logger_type = config.logger_type 87 @configured = true 88 end 89 90 require "mcollective/logger/#{logger_type.downcase}_logger" 91 92 logger_class = MCollective::Logger.const_get("#{logger_type.capitalize}_logger") 93 94 set_logger(logger_class.new) 95 else 96 set_logger(logger) 97 @configured = true 98 end 99 100 @logger.start 101 rescue Exception => e 102 @configured = false 103 STDERR.puts "Could not start logger: #{e.class} #{e}" 104 end
cycle_level()
click to toggle source
increments the active log level
# File lib/mcollective/log.rb 43 def cycle_level 44 @logger.cycle_level if @configured 45 end
debug(msg)
click to toggle source
Logs at debug level
# File lib/mcollective/log.rb 23 def debug(msg) 24 log(:debug, msg) 25 end
error(msg)
click to toggle source
Logs at error level
# File lib/mcollective/log.rb 33 def error(msg) 34 log(:error, msg) 35 end
execution_stack()
click to toggle source
this method is here to facilitate testing and from
# File lib/mcollective/log.rb 113 def execution_stack 114 caller 115 end
fatal(msg)
click to toggle source
Logs at fatal level
# File lib/mcollective/log.rb 28 def fatal(msg) 29 log(:fatal, msg) 30 end
from()
click to toggle source
figures out the filename that called us
# File lib/mcollective/log.rb 107 def from 108 path, line, method = execution_stack[3].split(/:(\d+)/) 109 "%s:%s%s" % [File.basename(path), line, method] 110 end
info(msg)
click to toggle source
Logs at info level
# File lib/mcollective/log.rb 13 def info(msg) 14 log(:info, msg) 15 end
instance()
click to toggle source
handle old code that relied on this class being a singleton
# File lib/mcollective/log.rb 38 def instance 39 self 40 end
log(level, msg)
click to toggle source
logs a message at a certain level
# File lib/mcollective/log.rb 55 def log(level, msg) 56 configure unless @configured 57 58 raise "Unknown log level" unless [:error, :fatal, :debug, :warn, :info].include?(level) 59 60 if @logger 61 @logger.log(level, from, msg) 62 else 63 t = Time.new.strftime("%H:%M:%S") 64 65 STDERR.puts "#{t}: #{level}: #{from}: #{msg}" 66 end 67 end
logger()
click to toggle source
Obtain the class name of the currently configured logger
# File lib/mcollective/log.rb 8 def logger 9 @logger.class 10 end
reopen()
click to toggle source
reopen log files
# File lib/mcollective/log.rb 48 def reopen 49 if @configured 50 @logger.reopen 51 end 52 end
set_logger(logger)
click to toggle source
sets the logger class to use
# File lib/mcollective/log.rb 70 def set_logger(logger) 71 @logger = logger 72 end
warn(msg)
click to toggle source
Logs at warn level
# File lib/mcollective/log.rb 18 def warn(msg) 19 log(:warn, msg) 20 end