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, 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"
        set_logger(eval("MCollective::Logger::#{logger_type.capitalize}_logger.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 43
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 23
def debug(msg)
    log(:debug, msg)
end
error(msg) click to toggle source

Logs at error level

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

Logs at fatal level

# File lib/mcollective/log.rb, line 28
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 98
def from
    from = File.basename(caller[2])
end
info(msg) click to toggle source

Logs at info level

# File lib/mcollective/log.rb, line 13
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 38
def instance
    self
end
log(level, msg) click to toggle source

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

Obtain the class name of the currently configured logger

# File lib/mcollective/log.rb, line 8
def logger
    @logger.class
end
set_logger(logger) click to toggle source

sets the logger class to use

# File lib/mcollective/log.rb, line 63
def set_logger(logger)
    @logger = logger
end
warn(msg) click to toggle source

Logs at warn level

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