Project

General

Profile

Bug #4833 » 0003-Add-documentation-to-WEBrick-BasicLog-and-WEBrick-Lo.patch

ouranos (Olivier Brisse), 06/06/2011 10:44 AM

View differences:

lib/webrick/log.rb
#
# $IPR: log.rb,v 1.26 2002/10/06 17:06:10 gotoyuzo Exp $
module WEBrick
module WEBrick #:nodoc:
##
# A generic logging class
class BasicLog
# log-level constant
# log-level constants
FATAL, ERROR, WARN, INFO, DEBUG = 1, 2, 3, 4, 5
# logger log-level
attr_accessor :level
##
......
end
end
##
# Closes the logger (also closes the log device associated to the logger)
def close
@log.close if @opened
@log = nil
end
##
# Logs +data+ at +level+
def log(level, data)
if @log && level <= @level
data += "\n" if /\n\Z/ !~ data
......
end
end
##
# Synonym for log(INFO, obj.to_s)
def <<(obj)
log(INFO, obj.to_s)
end
# Shortcut for logging a +FATAL+ message
def fatal(msg) log(FATAL, "FATAL " << format(msg)); end
# Shortcut for logging an +ERROR+ message
def error(msg) log(ERROR, "ERROR " << format(msg)); end
# Shortcut for logging a +WARN+ message
def warn(msg) log(WARN, "WARN " << format(msg)); end
# Shortcut for logging an +INFO+ message
def info(msg) log(INFO, "INFO " << format(msg)); end
# Shortcut for logging a +DEBUG+ message
def debug(msg) log(DEBUG, "DEBUG " << format(msg)); end
# Will the logger output +FATAL+ messages?
def fatal?; @level >= FATAL; end
# Will the logger output +ERROR+ messages?
def error?; @level >= ERROR; end
# Will the logger output +WARN+ messages?
def warn?; @level >= WARN; end
# Will the logger output +INFO+ messages?
def info?; @level >= INFO; end
# Will the logger output +DEBUG+ messages?
def debug?; @level >= DEBUG; end
private
##
# Formats +arg+ for the logger
#
# * If +arg+ is an +Exception+, it will format the error message and
# the back trace.
# * If +arg+ responds to #to_str, it will return it.
# * Otherwise it will return +arg+.inspect.
def format(arg)
if arg.is_a?(Exception)
"#{arg.class}: #{arg.message}\n\t" <<
......
# A logging class with timestamps
class Log < BasicLog
# Timestamp format
attr_accessor :time_format
##
# Same as BasicLog but prepend the timestamp to each message.
#
# The timestamp format is defined by +time_format+ and defaults to
# "[%Y-%m-%d %H:%M:%S]"
def initialize(log_file=nil, level=nil)
super(log_file, level)
@time_format = "[%Y-%m-%d %H:%M:%S]"
end
##
# See BasicLog#log
def log(level, data)
tmp = Time.now.strftime(@time_format)
tmp << " " << data
(1-1/2)