Class | MCollective::Log |
In: |
lib/mcollective/log.rb
|
Parent: | Object |
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 72: def configure(logger=nil) 73: unless logger 74: logger_type = "console" 75: 76: config = Config.instance 77: 78: if config.configured 79: logger_type = config.logger_type 80: @configured = true 81: end 82: 83: require "mcollective/logger/#{logger_type.downcase}_logger" 84: set_logger(eval("MCollective::Logger::#{logger_type.capitalize}_logger.new")) 85: else 86: set_logger(logger) 87: @configured = true 88: end 89: 90: 91: @logger.start 92: rescue Exception => e 93: @configured = false 94: STDERR.puts "Could not start logger: #{e.class} #{e}" 95: end
figures out the filename that called us
# File lib/mcollective/log.rb, line 98 98: def from 99: from = File.basename(caller[2]) 100: end
handle old code that relied on this class being a singleton
# File lib/mcollective/log.rb, line 38 38: def instance 39: self 40: end
logs a message at a certain level
# File lib/mcollective/log.rb, line 48 48: def log(level, msg) 49: configure unless @configured 50: 51: raise "Unknown log level" unless [:error, :fatal, :debug, :warn, :info].include?(level) 52: 53: if @logger 54: @logger.log(level, from, msg) 55: else 56: t = Time.new.strftime("%H:%M:%S") 57: 58: STDERR.puts "#{t}: #{level}: #{from}: #{msg}" 59: end 60: end