Module Swiftcore::Analogger::Client::LoggerInterface
In: lib/swiftcore/LoggerInterface.rb

Description

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.

Description From logger.rb:

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.

Example

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.

How to log a message

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.

  1. Message in block.
      logger.fatal { "Argument 'foo' not given." }
    
  2. Message as a string.
      logger.error "Argument #{ @foo } mismatch."
    
  3. With progname.
      logger.info('initialize') { "Initializing..." }
    
  4. With severity.
      logger.add(Logger::FATAL) { 'Fatal error!' }
    

Setting severity threshold

  1. Original interface.
      logger.sev_threshold = Logger::WARN
    
  2. Log4r (somewhat) compatible interface.
      logger.level = Logger::INFO
    
      DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
    

Methods

<<   debug   debug?   error   error?   extend_object   fatal   fatal?   info   info?   progname   progname=   unknown   warn   warn?  

Included Modules

Logger::Severity

Constants

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

External Aliases

level -> sev_threshold
level= -> sev_threshold=

Attributes

level  [RW]  Logging severity threshold (e.g. Logger::INFO).

Public Class methods

Public Instance methods

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 a DEBUG message.

See info for more information.

Returns true iff the current severity level allows for the printing of DEBUG messages.

Log an ERROR message.

See info for more information.

Returns true iff the current severity level allows for the printing of ERROR messages.

Log a FATAL message.

See info for more information.

Returns true iff the current severity level allows for the printing of FATAL messages.

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.

Examples

  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).

Return

See add.

Returns true iff the current severity level allows for the printing of INFO messages.

Log an UNKNOWN message. This will be printed no matter what the logger level.

See info for more information.

Log a WARN message.

See info for more information.

Returns true iff the current severity level allows for the printing of WARN messages.

[Validate]