Class | Logging::Logger |
In: |
lib/logging/logger.rb
|
Parent: | Object |
The Logger class is the primary interface to the Logging framework. It provides the logging methods that will be called from user methods, and it generates logging events that are sent to the appenders (the appenders take care of sending the log events to the logging destinations — files, sockets, etc).
Logger instances are obtained from the Repository and should not be directly created by users.
Example:
log = Logging.logger['my logger'] log.add_appenders( Logging.appenders.stdout ) # append to STDOUT log.level = :info # log 'info' and above log.info 'starting foo operation' ... log.info 'finishing foo operation' ... log.fatal 'unknown exception', exception
additive | [R] | |
name | [R] | |
parent | [R] | |
trace | [R] |
Returns the logger identified by name.
When name is a String or a Symbol it will be used "as is" to retrieve the logger. When name is a Class the class name will be used to retrieve the logger. When name is an object the name of the object‘s class will be used to retrieve the logger.
Example:
obj = MyClass.new log1 = Logger.new(obj) log2 = Logger.new(MyClass) log3 = Logger['MyClass'] log1.object_id == log2.object_id # => true log2.object_id == log3.object_id # => true
Log the given message without any formatting and without performing any level checks. The message is logged to all appenders. The message is passed up the logger tree if this logger‘s additivity is true.
Compares this logger by name to another logger. The normal return codes for String objects apply.
Log a message if the given severity is high enough. This is the generic logging method. Users will be more inclined to use debug, info, warn, error, and fatal.
Message format: message can be any object, but it has to be converted to a String in order to log it. The Logging::format_as method is used to determine how objects chould be converted to strings. Generally, inspect is used.
A special case is an Exception object, which will be printed in detail, including message, class, and backtrace.
If a message is not given, then the return value from the block is used as the message to log. This is useful when creating the actual message is an expensive operation. This allows the logger to check the severity against the configured level before actually constructing the message.
This method returns true if the message was logged, and false is returned if the message was not logged.
Add the given appenders to the list of appenders, where appenders can be either a single appender or an array of appenders.
Sets the additivity of the logger. Acceptable values are true, ‘true’, false, ‘false’, or nil. In this case nil does not change the additivity
Clears the current list of appenders and replaces them with app, where app can be either a single appender or an array of appenders.
Set the level for this logger. The level can be either a String, a Symbol, or a Fixnum. An ArgumentError is raised if this is not the case.
There are two special levels — "all" and "off". The former will enable log messages from this logger. The latter will disable all log messages from this logger.
Setting the logger level to nil will cause the parent‘s logger level to be used.
Example:
log.level = :debug log.level = "INFO" log.level = 4 log.level = 'off' log.level = :all
These prodcue an ArgumentError
log.level = Object log.level = -1 log.level = 1_000_000_000_000
Remove the given appenders from the list of appenders. The appenders to remove can be identified either by name using a String or by passing the appender instance. appenders can be a single appender or an array of appenders.
Sets the tracing of the logger. Acceptable values are true, ‘true’, false, ‘false’, or nil. In this case nil does not change the tracing.
Send the given event to the appenders for logging, and pass the event up to the parent if additive mode is enabled. The log level has already been checked before this method is called.
Set the parent logger for this logger. This method will be invoked by the Repository class when a parent or child is added to the hierarchy.