2011-05-09 15:22:41 -04:00
|
|
|
#--
|
2003-07-23 12:51:36 -04:00
|
|
|
# log.rb -- Log Class
|
|
|
|
#
|
|
|
|
# Author: IPR -- Internet Programming with Ruby -- writers
|
|
|
|
# Copyright (c) 2000, 2001 TAKAHASHI Masayoshi, GOTOU Yuuzou
|
|
|
|
# Copyright (c) 2002 Internet Programming with Ruby writers. All rights
|
|
|
|
# reserved.
|
|
|
|
#
|
|
|
|
# $IPR: log.rb,v 1.26 2002/10/06 17:06:10 gotoyuzo Exp $
|
|
|
|
|
|
|
|
module WEBrick
|
2011-05-09 20:13:58 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# A generic logging class
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class BasicLog
|
2011-06-16 17:12:00 -04:00
|
|
|
# log-level constants
|
2003-07-23 12:51:36 -04:00
|
|
|
FATAL, ERROR, WARN, INFO, DEBUG = 1, 2, 3, 4, 5
|
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
# log-level, messages above this level will be logged
|
2003-07-23 12:51:36 -04:00
|
|
|
attr_accessor :level
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
|
|
|
# Initializes a new logger for +log_file+ that outputs messages at +level+
|
|
|
|
# or higher. +log_file+ can be a filename, an IO-like object that
|
|
|
|
# responds to #<< or nil which outputs to $stderr.
|
|
|
|
#
|
|
|
|
# If no level is given INFO is chosen by default
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def initialize(log_file=nil, level=nil)
|
|
|
|
@level = level || INFO
|
|
|
|
case log_file
|
|
|
|
when String
|
|
|
|
@log = open(log_file, "a+")
|
|
|
|
@log.sync = true
|
|
|
|
@opened = true
|
|
|
|
when NilClass
|
|
|
|
@log = $stderr
|
|
|
|
else
|
|
|
|
@log = log_file # requires "<<". (see BasicLog#log)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
##
|
|
|
|
# Closes the logger (also closes the log device associated to the logger)
|
2003-07-23 12:51:36 -04:00
|
|
|
def close
|
|
|
|
@log.close if @opened
|
|
|
|
@log = nil
|
|
|
|
end
|
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
##
|
|
|
|
# Logs +data+ at +level+ if the given level is above the current log
|
|
|
|
# level.
|
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
def log(level, data)
|
|
|
|
if @log && level <= @level
|
2003-09-18 12:09:02 -04:00
|
|
|
data += "\n" if /\n\Z/ !~ data
|
|
|
|
@log << data
|
2003-07-23 12:51:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
##
|
|
|
|
# Synonym for log(INFO, obj.to_s)
|
2003-09-08 05:52:34 -04:00
|
|
|
def <<(obj)
|
|
|
|
log(INFO, obj.to_s)
|
|
|
|
end
|
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
# Shortcut for logging a FATAL message
|
2003-07-23 12:51:36 -04:00
|
|
|
def fatal(msg) log(FATAL, "FATAL " << format(msg)); end
|
2011-06-16 17:12:00 -04:00
|
|
|
# Shortcut for logging an ERROR message
|
2003-07-23 12:51:36 -04:00
|
|
|
def error(msg) log(ERROR, "ERROR " << format(msg)); end
|
2011-06-16 17:12:00 -04:00
|
|
|
# Shortcut for logging a WARN message
|
2003-07-23 12:51:36 -04:00
|
|
|
def warn(msg) log(WARN, "WARN " << format(msg)); end
|
2011-06-16 17:12:00 -04:00
|
|
|
# Shortcut for logging an INFO message
|
2003-07-23 12:51:36 -04:00
|
|
|
def info(msg) log(INFO, "INFO " << format(msg)); end
|
2011-06-16 17:12:00 -04:00
|
|
|
# Shortcut for logging a DEBUG message
|
2003-07-23 12:51:36 -04:00
|
|
|
def debug(msg) log(DEBUG, "DEBUG " << format(msg)); end
|
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
# Will the logger output FATAL messages?
|
2003-07-23 12:51:36 -04:00
|
|
|
def fatal?; @level >= FATAL; end
|
2011-06-16 17:12:00 -04:00
|
|
|
# Will the logger output ERROR messages?
|
2003-07-23 12:51:36 -04:00
|
|
|
def error?; @level >= ERROR; end
|
2011-06-16 17:12:00 -04:00
|
|
|
# Will the logger output WARN messages?
|
2003-07-23 12:51:36 -04:00
|
|
|
def warn?; @level >= WARN; end
|
2011-06-16 17:12:00 -04:00
|
|
|
# Will the logger output INFO messages?
|
2003-07-23 12:51:36 -04:00
|
|
|
def info?; @level >= INFO; end
|
2011-06-16 17:12:00 -04:00
|
|
|
# Will the logger output DEBUG messages?
|
2003-07-23 12:51:36 -04:00
|
|
|
def debug?; @level >= DEBUG; end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
##
|
|
|
|
# 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.
|
2003-07-23 12:51:36 -04:00
|
|
|
def format(arg)
|
2010-11-08 15:59:01 -05:00
|
|
|
if arg.is_a?(Exception)
|
2003-07-23 12:51:36 -04:00
|
|
|
"#{arg.class}: #{arg.message}\n\t" <<
|
2003-09-18 12:09:02 -04:00
|
|
|
arg.backtrace.join("\n\t") << "\n"
|
2003-07-23 12:51:36 -04:00
|
|
|
elsif arg.respond_to?(:to_str)
|
|
|
|
arg.to_str
|
|
|
|
else
|
|
|
|
arg.inspect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-09 20:13:58 -04:00
|
|
|
##
|
2011-06-16 17:12:00 -04:00
|
|
|
# A logging class that prepends a timestamp to each message.
|
2011-05-09 20:13:58 -04:00
|
|
|
|
2003-07-23 12:51:36 -04:00
|
|
|
class Log < BasicLog
|
2011-06-16 17:12:00 -04:00
|
|
|
# Format of the timestamp which is applied to each logged line. The
|
|
|
|
# default is <tt>"[%Y-%m-%d %H:%M:%S]"</tt>
|
2009-03-05 22:56:38 -05:00
|
|
|
attr_accessor :time_format
|
2003-07-23 12:51:36 -04:00
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
##
|
|
|
|
# Same as BasicLog#initialize
|
|
|
|
#
|
|
|
|
# You can set the timestamp format through #time_format
|
2003-07-23 12:51:36 -04:00
|
|
|
def initialize(log_file=nil, level=nil)
|
|
|
|
super(log_file, level)
|
|
|
|
@time_format = "[%Y-%m-%d %H:%M:%S]"
|
|
|
|
end
|
|
|
|
|
2011-06-16 17:12:00 -04:00
|
|
|
##
|
|
|
|
# Same as BasicLog#log
|
2003-07-23 12:51:36 -04:00
|
|
|
def log(level, data)
|
|
|
|
tmp = Time.now.strftime(@time_format)
|
|
|
|
tmp << " " << data
|
|
|
|
super(level, tmp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|