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



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

def configure(logger=nil)
  if logger
    set_logger(logger)
    @configured = true
  else
    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)
  end

  @logger.start
rescue Exception # rubocop:disable Lint/RescueException
  @configured = false
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



110
111
112
# File 'lib/mcollective/log.rb', line 110

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



104
105
106
107
# File 'lib/mcollective/log.rb', line 104

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



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

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")

    warn "#{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
# File 'lib/mcollective/log.rb', line 48

def reopen
  @logger.reopen if @configured
end

.set_logger(logger) ⇒ Object

sets the logger class to use



68
69
70
# File 'lib/mcollective/log.rb', line 68

def set_logger(logger) # rubocop:disable Naming/AccessorMethodName
  @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