Class: RightScale::Log
- Includes:
- Singleton
- Defined in:
- lib/right_agent/log.rb
Overview
Logs both to syslog and to local file
Defined Under Namespace
Classes: Formatter
Constant Summary collapse
- LEVELS_MAP =
Map of log levels symbols associated with corresponding Logger constant
{:debug => Logger::DEBUG, :info => Logger::INFO, :warn => Logger::WARN, :error => Logger::ERROR, :fatal => Logger::FATAL}
- @@inverted_levels_map =
nil
Instance Method Summary collapse
-
#add_logger(logger) ⇒ Object
Add new logger to list of multiplexed loggers.
-
#error(description, exception = nil, backtrace = :caller) ⇒ Object
Log error and optionally append exception information.
-
#facility=(facility) ⇒ Object
Sets the syslog facility that will be used when emitting syslog messages.
-
#force_debug ⇒ Object
Force log level to debug and disregard any further attempt to change it.
-
#force_logger(logger) ⇒ Object
Force use of given logger and override all defaults.
-
#format(description, exception = nil, backtrace = :caller) ⇒ Object
Format error information.
-
#initialize ⇒ Log
constructor
A new instance of Log.
-
#initialized ⇒ Object
Was logger initialized?.
-
#level ⇒ Object
Current log level.
-
#level=(level) ⇒ Object
Sets the level for the Logger by symbol or by Logger constant.
-
#level_from_sym(sym) ⇒ Object
Map symbol log level to Logger constant.
-
#level_to_sym(lvl) ⇒ Object
Map Logger log level constant to symbol.
-
#log_to_file_only(val) ⇒ Object
Set whether syslog should be used or to log to an agent-specific file This should be called before anything else.
-
#logger ⇒ Object
Read access to internal multiplexer.
-
#method_missing(m, *args) ⇒ Object
Forward all method calls to underlying Logger object created with init Return the result of only the first registered logger to keep the interface consistent with that of a Logger.
-
#notify(callback) ⇒ Object
Register callback to be activated when there is a logging configuration change Currently the only logging change reported is log level.
-
#program_name=(prog_name) ⇒ Object
Sets the syslog program name that will be reported Can only be successfully called before logging is initialized.
-
#remove_logger(logger) ⇒ Object
Remove logger from list of multiplexed loggers.
-
#respond_to?(m) ⇒ Boolean
Determine whether this object, or its method_missing proxy, responds to the given method name.
-
#warning(description, exception = nil, backtrace = :caller) ⇒ Object
(also: #warn)
Log warning and optionally append exception information.
Methods included from Singleton
Constructor Details
#initialize ⇒ Log
Returns a new instance of Log.
113 114 115 116 |
# File 'lib/right_agent/log.rb', line 113 def initialize # Was log ever used? @initialized = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
Forward all method calls to underlying Logger object created with init Return the result of only the first registered logger to keep the interface consistent with that of a Logger
Parameters
- m(Symbol)
-
Forwarded method name
- args(Array)
-
Forwarded method arguments
Return
- (Object)
-
Result from first registered logger
128 129 130 131 132 |
# File 'lib/right_agent/log.rb', line 128 def method_missing(m, *args) init unless @initialized @logger.level = level_from_sym(level) if @level_frozen @logger.__send__(m, *args) end |
Instance Method Details
#add_logger(logger) ⇒ Object
Add new logger to list of multiplexed loggers
Parameters
- logger(Object)
-
Logger that should get log messages
Return
- @logger(RightScale::Multiplexer)
-
Multiplexer logger
255 256 257 258 259 |
# File 'lib/right_agent/log.rb', line 255 def add_logger(logger) init unless @initialized logger.level = level_from_sym(Log.instance.level) @logger.add(logger) end |
#error(description, exception = nil, backtrace = :caller) ⇒ Object
Log error and optionally append exception information
Parameters
- description(String)
-
Error description
- exception(Exception|String)
-
Associated exception or other parenthetical error information
- backtrace(Symbol)
-
Exception backtrace extent: :no_trace, :caller, or :trace,
defaults to :caller
Return
- (Object)
-
Result from first registered logger
176 177 178 179 |
# File 'lib/right_agent/log.rb', line 176 def error(description, exception = nil, backtrace = :caller) init unless @initialized @logger.error(format(description, exception, backtrace)) end |
#facility=(facility) ⇒ Object
Sets the syslog facility that will be used when emitting syslog messages. Can only be successfully called before logging is initialized
Parameters
- facility(String)
-
A syslog facility name, e.g. ‘user’ or ‘local0’
Return
- program_name(String)
-
The input string
Raise
- RuntimeError
-
If logger is already initialized
325 326 327 328 |
# File 'lib/right_agent/log.rb', line 325 def facility=(facility) raise 'Logger already initialized' if @initialized @facility = facility end |
#force_debug ⇒ Object
Force log level to debug and disregard any further attempt to change it
Return
- true
-
Always return true
388 389 390 391 |
# File 'lib/right_agent/log.rb', line 388 def force_debug self.level = :debug @level_frozen = true end |
#force_logger(logger) ⇒ Object
Force use of given logger and override all defaults
Parameters
- logger(Logger)
-
Logger compatible object
Return
- true
-
Always return true
400 401 402 403 |
# File 'lib/right_agent/log.rb', line 400 def force_logger(logger) @initialized = true @logger = logger end |
#format(description, exception = nil, backtrace = :caller) ⇒ Object
Format error information
Parameters
- description(String)
-
Error description
- exception(Exception|String)
-
Associated exception or other parenthetical error information
- backtrace(Symbol)
-
Exception backtrace extent: :no_trace, :caller, or :trace,
defaults to :caller
Return
- (Object)
-
Result from first registered logger
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/right_agent/log.rb', line 191 def format(description, exception = nil, backtrace = :caller) if exception if exception.respond_to?(:message) description += " (#{exception.class}: #{exception.}" else description += " (#{exception}" backtrace = :no_trace end case backtrace when :no_trace then description += ")" when :caller then description += " in " + exception.backtrace[0] + ")" when :trace then description += " in\n " + exception.backtrace.join("\n ") + ")" end end description end |
#initialized ⇒ Object
Was logger initialized?
Return
- true
-
if logger has been initialized
- false
-
Otherwise
292 293 294 |
# File 'lib/right_agent/log.rb', line 292 def initialized @initialized end |
#level ⇒ Object
Current log level
Return
- level(Symbol)
-
One of :debug, :info, :warn, :error or :fatal
364 365 366 367 |
# File 'lib/right_agent/log.rb', line 364 def level init unless @initialized level = level_to_sym(@level) end |
#level=(level) ⇒ Object
Sets the level for the Logger by symbol or by Logger constant
Parameters
- level(Object)
-
One of :debug, :info, :warn, :error, :fatal or one of “debug”, “info”, “warn”, “error”, “fatal” or one of Logger::INFO … Logger::FATAL
Return
- level(Symbol)
-
New log level, or current level if frozen
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/right_agent/log.rb', line 339 def level=(level) init unless @initialized unless @level_frozen new_level = case level when Symbol then level_from_sym(level) when String then level_from_sym(level.to_sym) else level end if new_level != @level @logger.info("[setup] Setting log level to #{level_to_sym(new_level).to_s.upcase}") @logger.level = @level = new_level end # Notify even if unchanged since don't know when callback was set @notify.each { |n| n.call(@level) } if @notify end level = level_to_sym(@level) end |
#level_from_sym(sym) ⇒ Object
Map symbol log level to Logger constant
Parameters
- sym(Symbol)
-
Log level symbol, one of :debug, :info, :warn, :error or :fatal
Return
- lvl(Constant)
-
One of Logger::DEBUG … Logger::FATAL
Raise
- (RightScale::Exceptions::Argument)
-
if level symbol is invalid
218 219 220 221 |
# File 'lib/right_agent/log.rb', line 218 def level_from_sym(sym) raise Exceptions::Argument, "Invalid log level symbol :#{sym}" unless LEVELS_MAP.include?(sym) lvl = LEVELS_MAP[sym] end |
#level_to_sym(lvl) ⇒ Object
Map Logger log level constant to symbol
Parameters
- lvl(Constant)
-
Log level constant, one of Logger::DEBUG … Logger::FATAL
Return
- sym(Symbol)
-
One of :debug, :info, :warn, :error or :fatal
Raise
- (RightScale::Exceptions::Argument)
-
if level is invalid
233 234 235 236 237 |
# File 'lib/right_agent/log.rb', line 233 def level_to_sym(lvl) @@inverted_levels_map ||= LEVELS_MAP.invert raise Exceptions::Argument, "Invalid log level: #{lvl}" unless @@inverted_levels_map.include?(lvl) sym = @@inverted_levels_map[lvl] end |
#log_to_file_only(val) ⇒ Object
Set whether syslog should be used or to log to an agent-specific file This should be called before anything else
Parameters
- val(Boolean)
-
Whether syslog should be used (false) or a agent-specific log file (true)
Raise
- RuntimeError
-
If logger is already initialized
282 283 284 285 |
# File 'lib/right_agent/log.rb', line 282 def log_to_file_only(val) raise 'Logger already initialized' if @initialized @log_to_file_only = !!val end |
#logger ⇒ Object
Read access to internal multiplexer
Return
- logger(RightScale::Multiplexer)
-
Multiplexer logger
243 244 245 246 |
# File 'lib/right_agent/log.rb', line 243 def logger init unless @initialized logger = @logger end |
#notify(callback) ⇒ Object
Register callback to be activated when there is a logging configuration change Currently the only logging change reported is log level
Parameters
- callback(Proc)
-
Block to be activated with following parameter when log level changes:
- log_level(Symbol)
-
Current log level
Return
- true
-
Always return true
378 379 380 381 |
# File 'lib/right_agent/log.rb', line 378 def notify(callback) @notify = (@notify ||= []) << callback true end |
#program_name=(prog_name) ⇒ Object
Sets the syslog program name that will be reported Can only be successfully called before logging is initialized
Parameters
- prog_name(String)
-
An arbitrary string, or “nil” to use
the default name that is based on the agent's identity
Return
- program_name(String)
-
The input string
Raise
- RuntimeError
-
If logger is already initialized
309 310 311 312 |
# File 'lib/right_agent/log.rb', line 309 def program_name=(prog_name) raise 'Logger already initialized' if @initialized @program_name = prog_name end |
#remove_logger(logger) ⇒ Object
Remove logger from list of multiplexed loggers
Parameters
- logger(Object)
-
Logger to be removed
Return
- @logger(RightScale::Multiplexer)
-
Multiplexer logger
268 269 270 271 |
# File 'lib/right_agent/log.rb', line 268 def remove_logger(logger) init unless @initialized @logger.remove(logger) end |
#respond_to?(m) ⇒ Boolean
Determine whether this object, or its method_missing proxy, responds to the given method name. This follows the best practice of always overriding #respond_to? whenever one implements dynamic dispatch via #method_missing.
Parameters
- m(Symbol)
-
Forwarded method name
Return
- (true|false)
-
True if this object or its proxy responds to the names method, false otherwise
144 145 146 147 |
# File 'lib/right_agent/log.rb', line 144 def respond_to?(m) init unless @initialized super(m) || @logger.respond_to?(m) end |
#warning(description, exception = nil, backtrace = :caller) ⇒ Object Also known as: warn
Log warning and optionally append exception information
Parameters
- description(String)
-
Error description
- exception(Exception|String)
-
Associated exception or other parenthetical error information
- backtrace(Symbol)
-
Exception backtrace extent: :no_trace, :caller, or :trace,
defaults to :caller
Return
- (Object)
-
Result from first registered logger
159 160 161 162 |
# File 'lib/right_agent/log.rb', line 159 def warning(description, exception = nil, backtrace = :caller) init unless @initialized @logger.warn(format(description, exception, backtrace)) end |