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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mcollective/log.rb', line 79

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"

    logger_class = MCollective::Logger.const_get("#{logger_type.capitalize}_logger")

    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_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

.execution_stackObject

this method is here to facilitate testing and from



113
114
115
# File 'lib/mcollective/log.rb', line 113

def execution_stack
  caller
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



107
108
109
110
# File 'lib/mcollective/log.rb', line 107

def from
  path, line, method = execution_stack[3].split(/:(\d+)/)
  "%s:%s%s" % [File.basename(path), line, method]
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



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mcollective/log.rb', line 55

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

.reopenObject

reopen log files



48
49
50
51
52
# File 'lib/mcollective/log.rb', line 48

def reopen
  if @configured
    @logger.reopen
  end
end

.set_logger(logger) ⇒ Object

sets the logger class to use



70
71
72
# File 'lib/mcollective/log.rb', line 70

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