Class: Facter::Log
- Inherits:
-
Object
- Object
- Facter::Log
- Defined in:
- lib/facter/framework/logging/logger.rb
Constant Summary collapse
- @@logger =
nil
- @@message_callback =
nil
- @@has_errors =
false
- @@debug_messages =
Set.new
- @@warn_messages =
Set.new
- @@timing =
false
Class Method Summary collapse
- .clear_messages ⇒ Object
- .errors? ⇒ Boolean
- .level ⇒ Object
- .level=(log_level) ⇒ Object
- .on_message(&block) ⇒ Object
- .output(output) ⇒ Object
- .set_logger_format ⇒ Object
-
.show_time(string) ⇒ void
private
Print timing information.
-
.timing(bool) ⇒ void
private
Enable or disable logging of timing information.
-
.timing? ⇒ Boolean
private
Returns whether timing output is turned on.
Instance Method Summary collapse
- #debug(msg) ⇒ Object
- #debugonce(msg) ⇒ Object
-
#error(msg, colorize = false) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter.
- #info(msg) ⇒ Object
-
#initialize(logged_class) ⇒ Log
constructor
A new instance of Log.
- #log_exception(exception) ⇒ Object
- #warn(msg) ⇒ Object
- #warnonce(message) ⇒ Object
Constructor Details
#initialize(logged_class) ⇒ Log
Returns a new instance of Log.
96 97 98 99 100 101 102 |
# File 'lib/facter/framework/logging/logger.rb', line 96 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
.clear_messages ⇒ Object
24 25 26 27 |
# File 'lib/facter/framework/logging/logger.rb', line 24 def @@debug_messages.clear @@warn_messages.clear end |
.errors? ⇒ Boolean
41 42 43 |
# File 'lib/facter/framework/logging/logger.rb', line 41 def errors? @@has_errors end |
.level ⇒ Object
37 38 39 |
# File 'lib/facter/framework/logging/logger.rb', line 37 def level @@logger.level end |
.level=(log_level) ⇒ Object
33 34 35 |
# File 'lib/facter/framework/logging/logger.rb', line 33 def level=(log_level) @@logger.level = log_level end |
.on_message(&block) ⇒ Object
29 30 31 |
# File 'lib/facter/framework/logging/logger.rb', line 29 def (&block) @@message_callback = block end |
.output(output) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/facter/framework/logging/logger.rb', line 45 def output(output) return if @@logger @@logger = Logger.new(output) set_logger_format @@logger.level = DEFAULT_LOG_LEVEL end |
.set_logger_format ⇒ Object
53 54 55 56 57 58 |
# File 'lib/facter/framework/logging/logger.rb', line 53 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 |
.show_time(string) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Print timing information
68 69 70 71 72 73 74 75 76 |
# File 'lib/facter/framework/logging/logger.rb', line 68 def show_time(string) return unless string && timing? if @@message_callback @@message_callback.call(:info, string) else warn("#{GREEN}#{string}#{RESET}") end end |
.timing(bool) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Enable or disable logging of timing information
84 85 86 |
# File 'lib/facter/framework/logging/logger.rb', line 84 def timing(bool) @@timing = bool end |
.timing? ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns whether timing output is turned on
91 92 93 |
# File 'lib/facter/framework/logging/logger.rb', line 91 def timing? @@timing end |
Instance Method Details
#debug(msg) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/facter/framework/logging/logger.rb', line 104 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 |
#debugonce(msg) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/facter/framework/logging/logger.rb', line 115 def debugonce(msg) return unless debugging_active? = msg.to_s return if @@debug_messages.include? @@debug_messages << debug() end |
#error(msg, colorize = false) ⇒ Object
rubocop:disable Style/OptionalBooleanParameter
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/facter/framework/logging/logger.rb', line 153 def error(msg, colorize = false) # rubocop:disable Style/OptionalBooleanParameter @@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
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/facter/framework/logging/logger.rb', line 125 def info(msg) if msg.nil? || msg.empty? (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
164 165 166 167 168 169 |
# File 'lib/facter/framework/logging/logger.rb', line 164 def log_exception(exception) msg = exception. msg += "\n#{exception.backtrace.join("\n")}" if Options[:trace] error(msg, true) end |
#warn(msg) ⇒ Object
136 137 138 139 140 141 142 143 |
# File 'lib/facter/framework/logging/logger.rb', line 136 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 |
#warnonce(message) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/facter/framework/logging/logger.rb', line 145 def warnonce() = .to_s return if @@warn_messages.include? @@warn_messages << warn() end |