TeensyMud - 'A ruby mud server.'

LogClass

How to use the LogClass

The Log class is used in a class if you want to write to the logs. The 'log' method is inserted into a class by invoking the logger method at the class level.

There are two log outputters 'server' and 'stderr'. The 'server' outputter points to whatever logfile is defined to in the configuration file (config.yaml) file. If it's undefined it points to 'logs/server.log'. The 'stderr' outputter is the console.

There are five levels of logging.

DEBUG < INFO < WARN < ERROR < FATAL

Each level includes those above it. Thus DEBUG is everything. DEBUG is the default if unspecified

A sample program and output follows:

$:.unshift "lib"
require 'log'

class A
  logger                         # defaults
  def initialize
    log.info "A initialized"
    raise "fatal error!"
  rescue
    log.fatal $!
  end
end

A.new

class B
  logger 'WARN', ['stderr']      # Only warning level and higher and just to the console
  def initialize
    log.info "B initialized"
    raise "fatal error!"
  rescue
    log.fatal $!
  end
end

B.new

What appears on the console:

06-01-12 15:07:38 [ INFO] (A) A initialized
06-01-12 15:07:38 [FATAL] (A) Caught RuntimeError: fatal error!
    C:/work/teensymud/trunk/testlog.rb:21:in `initialize'
    C:/work/teensymud/trunk/testlog.rb:27:in `new'
    C:/work/teensymud/trunk/testlog.rb:27
06-01-12 15:07:38 [FATAL] (B) Caught RuntimeError: fatal error!
    C:/work/teensymud/trunk/testlog.rb:33:in `initialize'
    C:/work/teensymud/trunk/testlog.rb:39:in `new'
    C:/work/teensymud/trunk/testlog.rb:39

What appears in logs/server.log:

06-01-12 15:07:38 [FATAL] (A) Caught RuntimeError: fatal error!
    C:/work/teensymud/trunk/testlog.rb:21:in `initialize'
    C:/work/teensymud/trunk/testlog.rb:27:in `new'
    C:/work/teensymud/trunk/testlog.rb:27