class MCollective::Log

A simple class that allows logging at various levels.

Constants

VALID_LEVELS

Public Class Methods

check_level(level) click to toggle source
# File lib/mcollective/log.rb, line 55
def check_level(level)
  raise "Unknown log level" unless valid_level?(level)
end
config_and_check_level(level) click to toggle source
# File lib/mcollective/log.rb, line 49
def config_and_check_level(level)
  configure unless @configured
  check_level(level)
  @logger.should_log?(level)
end
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, line 130
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/%s_logger" % logger_type.downcase

    logger_class = MCollective::Logger.const_get("%s_logger" % logger_type.capitalize)

    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
cycle_level() click to toggle source

increments the active log level

# File lib/mcollective/log.rb, line 45
def cycle_level
  @logger.cycle_level if @configured
end
debug(msg) click to toggle source

Logs at debug level

# File lib/mcollective/log.rb, line 25
def debug(msg)
  log(:debug, msg)
end
error(msg) click to toggle source

Logs at error level

# File lib/mcollective/log.rb, line 35
def error(msg)
  log(:error, msg)
end
execution_stack() click to toggle source

this method is here to facilitate testing

# File lib/mcollective/log.rb, line 170
def execution_stack
  caller
end
fatal(msg) click to toggle source

Logs at fatal level

# File lib/mcollective/log.rb, line 30
def fatal(msg)
  log(:fatal, msg)
end
from() click to toggle source

figures out the filename that called us

# File lib/mcollective/log.rb, line 164
def from
  path, line, method = execution_stack[3].split(/:(\d+)/)
  "%s:%s%s" % [File.basename(path), line, method]
end
info(msg) click to toggle source

Logs at info level

# File lib/mcollective/log.rb, line 15
def info(msg)
  log(:info, msg)
end
instance() click to toggle source

handle old code that relied on this class being a singleton

# File lib/mcollective/log.rb, line 40
def instance
  self
end
log(level, msg, origin=nil) click to toggle source

logs a message at a certain level

# File lib/mcollective/log.rb, line 106
def log(level, msg, origin=nil)
  return unless config_and_check_level(level)

  origin = from unless origin

  if @logger
    @logger.log(level, origin, msg)
  else
    t = Time.new.strftime("%H:%M:%S")

    STDERR.puts "#{t}: #{level}: #{origin}: #{msg}"
  end
end
logexception(msgid, level, e, backtrace=false, args={}) click to toggle source
# File lib/mcollective/log.rb, line 67
def logexception(msgid, level, e, backtrace=false, args={})
  return false unless config_and_check_level(level)

  path, line, method = e.backtrace[1].split(/:(\d+)/)
  origin = "%s:%s%s" % [File.basename(path), line, method]

  if e.is_a?(CodedError)
    msg = "%s: %s" % [e.code, e.to_s]
  else
    error_string = "%s: %s" % [e.class, e.to_s]
    msg = message_for(msgid, args.merge(:error => error_string))
  end

  log(level, msg, origin)

  if backtrace
    e.backtrace.each do |line|
      log(level, "%s:          %s" % [msgid, line], origin)
    end
  end
end
logger() click to toggle source

Obtain the class name of the currently configured logger

# File lib/mcollective/log.rb, line 10
def logger
  @logger.class
end
logmsg(msgid, default, level, args={}) click to toggle source

Logs a message at a certain level, the message must be a token that will be looked up from the i18n localization database

Messages can interprolate strings from the args hash, a message with “foo %{bar}” in the localization database will use args for the value there, the interprolation is handled by the i18n library itself

# File lib/mcollective/log.rb, line 97
def logmsg(msgid, default, level, args={})
  return false unless config_and_check_level(level)

  msg = message_for(msgid, {:default => default}.merge(args))

  log(level, msg)
end
message_for(msgid, args={}) click to toggle source
# File lib/mcollective/log.rb, line 63
def message_for(msgid, args={})
  "%s: %s" % [msgid, Util.t(msgid, args)]
end
set_logger(logger) click to toggle source

sets the logger class to use

# File lib/mcollective/log.rb, line 121
def set_logger(logger)
  @logger = logger
end
unconfigure() click to toggle source
# File lib/mcollective/log.rb, line 158
def unconfigure
  @configured = false
  set_logger(nil)
end
valid_level?(level) click to toggle source
# File lib/mcollective/log.rb, line 59
def valid_level?(level)
  VALID_LEVELS.include?(level)
end
warn(msg) click to toggle source

Logs at warn level

# File lib/mcollective/log.rb, line 20
def warn(msg)
  log(:warn, msg)
end