Class: Facter::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/facter/framework/logging/logger.rb

Constant Summary collapse

@@logger =
nil
@@message_callback =
nil
@@has_errors =
false

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logged_class) ⇒ Log

Returns a new instance of Log.



52
53
54
55
56
57
58
# File 'lib/facter/framework/logging/logger.rb', line 52

def initialize(logged_class)
  @class_name = LoggerHelper.determine_callers_name(logged_class)
  return unless @@logger.nil?

  @@logger = Logger.new(STDOUT)
  @@logger.level = DEFAULT_LOG_LEVEL
end

Class Method Details

.errors?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/facter/framework/logging/logger.rb', line 32

def errors?
  @@has_errors
end

.levelObject



28
29
30
# File 'lib/facter/framework/logging/logger.rb', line 28

def level
  @@logger.level
end

.level=(log_level) ⇒ Object



24
25
26
# File 'lib/facter/framework/logging/logger.rb', line 24

def level=(log_level)
  @@logger.level = log_level
end

.on_message(&block) ⇒ Object



20
21
22
# File 'lib/facter/framework/logging/logger.rb', line 20

def on_message(&block)
  @@message_callback = block
end

.output(output) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/facter/framework/logging/logger.rb', line 36

def output(output)
  return if @@logger

  @@logger = Logger.new(output)
  set_logger_format
  @@logger.level = DEFAULT_LOG_LEVEL
end

.set_logger_formatObject



44
45
46
47
48
49
# File 'lib/facter/framework/logging/logger.rb', line 44

def set_logger_format
  @@logger.formatter = proc do |severity, datetime, _progname, msg|
    datetime = datetime.strftime(@datetime_format || '%Y-%m-%d %H:%M:%S.%6N ')
    "[#{datetime}] #{severity} #{msg} \n"
  end
end

Instance Method Details

#debug(msg) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/facter/framework/logging/logger.rb', line 60

def debug(msg)
  return unless debugging_active?

  if @@message_callback && Options[:allow_external_loggers]
    @@message_callback.call(:debug, msg)
  else
    msg = colorize(msg, CYAN) if Options[:color]
    @@logger.debug(@class_name + ' - ' + msg)
  end
end

#error(msg, colorize = false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/facter/framework/logging/logger.rb', line 91

def error(msg, colorize = false)
  @@has_errors = true

  if @@message_callback && Options[:allow_external_loggers]
    @@message_callback.call(:error, msg)
  else
    msg = colorize(msg, RED) if colorize || Options[:color]
    @@logger.error(@class_name + ' - ' + msg)
  end
end

#info(msg) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/facter/framework/logging/logger.rb', line 71

def info(msg)
  if msg.nil? || msg.empty?
    empty_message_error(msg)
  elsif @@message_callback && Options[:allow_external_loggers]
    @@message_callback.call(:info, msg)
  else
    msg = colorize(msg, GREEN) if Options[:color]
    @@logger.info(@class_name + ' - ' + msg)
  end
end

#log_exception(exception) ⇒ Object



102
103
104
105
106
107
# File 'lib/facter/framework/logging/logger.rb', line 102

def log_exception(exception)
  msg = exception.message
  msg += "\n" + exception.backtrace.join("\n") if Options[:trace]

  error(msg, true)
end

#warn(msg) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/facter/framework/logging/logger.rb', line 82

def warn(msg)
  if @@message_callback && Options[:allow_external_loggers]
    @@message_callback.call(:warn, msg)
  else
    msg = colorize(msg, YELLOW) if Options[:color]
    @@logger.warn(@class_name + ' - ' + msg)
  end
end