Class: MCollective::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/log.rb

Overview

A simple class that allows logging at various levels.

Class Method Summary collapse

Class Method Details

.configure(logger = nil) ⇒ Object

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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_levelObject

increments the active log level



43
44
45
# File 'lib/mcollective/log.rb', line 43

def cycle_level
  @logger.cycle_level if @configured
end

.debug(msg) ⇒ Object

Logs at debug level



23
24
25
# File 'lib/mcollective/log.rb', line 23

def debug(msg)
  log(:debug, msg)
end

.error(msg) ⇒ Object

Logs at error level



33
34
35
# File 'lib/mcollective/log.rb', line 33

def error(msg)
  log(:error, msg)
end

.fatal(msg) ⇒ Object

Logs at fatal level



28
29
30
# File 'lib/mcollective/log.rb', line 28

def fatal(msg)
  log(:fatal, msg)
end

.fromObject

figures out the filename that called us



98
99
100
# File 'lib/mcollective/log.rb', line 98

def from
  from = File.basename(caller[2])
end

.info(msg) ⇒ Object

Logs at info level



13
14
15
# File 'lib/mcollective/log.rb', line 13

def info(msg)
  log(:info, msg)
end

.instanceObject

handle old code that relied on this class being a singleton



38
39
40
# File 'lib/mcollective/log.rb', line 38

def instance
  self
end

.log(level, msg) ⇒ Object

logs a message at a certain level



48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

.loggerObject

Obtain the class name of the currently configured logger



8
9
10
# File 'lib/mcollective/log.rb', line 8

def logger
  @logger.class
end

.set_logger(logger) ⇒ Object

sets the logger class to use



63
64
65
# File 'lib/mcollective/log.rb', line 63

def set_logger(logger)
  @logger = logger
end

.warn(msg) ⇒ Object

Logs at warn level



18
19
20
# File 'lib/mcollective/log.rb', line 18

def warn(msg)
  log(:warn, msg)
end