Class: Chef::Expander::Logger

Inherits:
Logger
  • Object
show all
Defined in:
lib/chef/expander/logger.rb

Overview

Customized Logger class that dispenses with the unnecessary mutexing. As long as you write one line at a time, the OS will take care of keeping your output in order. Expander commonly runs as a cluster of worker processes so the mutexing wasn’t actually helping us anyway.

We don’t use the program name field in the logger, so support for that has been removed. The log format is also hardcoded since we don’t ever change the format.

Constant Summary collapse

LEVELS =
{ :debug=>DEBUG, :info=>INFO, :warn=>WARN, :error=>ERROR, :fatal=>FATAL}
LEVEL_INTEGERS =
LEVELS.invert
LEVEL_TO_STR =
Hash[LEVEL_INTEGERS.map {|i,sym| [i,sym.to_s.upcase]
LOG_DEVICES =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_device) ⇒ Logger

Returns a new instance of Logger.



57
58
59
60
# File 'lib/chef/expander/logger.rb', line 57

def initialize(log_device)
  @level = DEBUG
  init(log_device)
end

Instance Attribute Details

#log_deviceObject (readonly)

Returns the value of attribute log_device.



50
51
52
# File 'lib/chef/expander/logger.rb', line 50

def log_device
  @log_device
end

Instance Method Details

#<<(msg) ⇒ Object



72
73
74
# File 'lib/chef/expander/logger.rb', line 72

def <<(msg)
  @log_device.print(msg)
end

#add(severity = UNKNOWN, message = nil, progname = nil, &block) ⇒ Object Also known as: log



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chef/expander/logger.rb', line 76

def add(severity=UNKNOWN, message = nil, progname = nil, &block)
  return true unless severity >= @level

  message ||= progname # level methods (e.g, #debug) pass explicit message as progname

  if message.nil? && block_given?
    message = yield
  end

  self << sprintf("[%s] %s: %s\n", Time.new.rfc2822(), LEVEL_TO_STR[severity], msg2str(message))
  true
end

#init(log_device) ⇒ Object

(re-)initialize the Logger with a new IO object or file to log to.



53
54
55
# File 'lib/chef/expander/logger.rb', line 53

def init(log_device)
  @log_device = initialize_log_device(log_device)
end

#level=(new_level) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/chef/expander/logger.rb', line 62

def level=(new_level)
  @level =  if new_level.kind_of?(Fixnum) && LEVEL_INTEGERS.key?(new_level)
    new
  elsif LEVELS.key?(new_level)
    LEVELS[new_level]
  else
    raise InvalidLogLevel, "#{new_level} is not a valid log level. Valid log levels are [#{LEVEL_INTEGERS.keys.join(',')}] and [#{LEVELS.join(',')}]"
  end
end