| Module | Swiftcore::Analogger::Client::LoggerInterface |
| In: |
lib/swiftcore/LoggerInterface.rb
|
LoggerInterface provides a module which may be used to extend an Analogger Client interface, and provide a dual-mode interface, supporting both the analogger client api, and the logger api.
The Logger class provides a simple but sophisticated logging utility that anyone can use because it‘s included in the Ruby 1.8.x standard library.
The HOWTOs below give a code-based overview of Logger‘s usage, but the basic concept is as follows. You create a Logger object (output to a file or elsewhere), and use it to log messages. The messages will have varying levels (info, error, etc), reflecting their varying importance. The levels, and their meanings, are:
| FATAL: | an unhandleable error that results in a program crash |
| ERROR: | a handleable error condition |
| WARN: | a warning |
| INFO: | generic (useful) information about system operation |
| DEBUG: | low-level information for developers |
So each message has a level, and the Logger itself has a level, which acts as a filter, so you can control the amount of information emitted from the logger without having to remove actual messages.
For instance, in a production system, you may have your logger(s) set to INFO (or WARN if you don‘t want the log files growing large with repetitive information). When you are developing it, though, you probably want to know about the program‘s internal state, and would set them to DEBUG.
A simple example demonstrates the above explanation:
log = Swiftcore::Analogger::Client.new('logger_interface','127.0.0.1','47990')
log.extend(Swiftcore::Analogger::Client::LoggerInterface)
log.level = Logger::WARN
log.debug("Created logger")
log.info("Program started")
log.warn("Nothing to do!")
begin
File.each_line(path) do |line|
unless line =~ /^(\w+) = (.*)$/
log.error("Line in wrong format: #{line}")
end
end
rescue => err
log.fatal("Caught exception; exiting")
log.fatal(err)
end
Because the Logger‘s level is set to WARN, only the warning, error, and fatal messages are recorded. The debug and info messages are silently discarded.
Notice the different methods (fatal, error, info) being used to log messages of various levels. Other methods in this family are warn and debug. add is used below to log a message of an arbitrary (perhaps dynamic) level.
logger.fatal { "Argument 'foo' not given." }
logger.error "Argument #{ @foo } mismatch."
logger.info('initialize') { "Initializing..." }
logger.add(Logger::FATAL) { 'Fatal error!' }
logger.sev_threshold = Logger::WARN
logger.level = Logger::INFO DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
| MapUnknownTo | = | 'info' | ||
| SeverityToLevel | = | Hash.new | A severity is the worded name for a log level A level is the numerical value given to a severity | |
| LevelToSeverity | = | Hash.new |
| level | -> | sev_threshold |
| level= | -> | sev_threshold= |
| level | [RW] | Logging severity threshold (e.g. Logger::INFO). |
As there is no notion of a raw message for an analogger client, this sends messages at the default log level (unknown, which is mapped to MapUnknownTo).
Log an INFO message.
The message can come either from the progname argument or the block. If both are provided, then the block is used as the message, and progname is used as the program name.
logger.info("MainApp") { "Received connection from #{ip}" }
# ...
logger.info "Waiting for input from user"
# ...
logger.info { "User typed #{input}" }
You‘ll probably stick to the second form above, unless you want to provide a program name (which you can do with Logger#progname= as well).
See add.
Log an UNKNOWN message. This will be printed no matter what the logger level.
See info for more information.