diff options
author | gotoyuzo <gotoyuzo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-23 16:51:36 +0000 |
---|---|---|
committer | gotoyuzo <gotoyuzo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-23 16:51:36 +0000 |
commit | 01eba908adcd150a7b816af0dbe167c4c4912a90 (patch) | |
tree | 0a0d7cc4e66db930a7f74fd1466338979dae91c2 /lib/webrick/log.rb | |
parent | 02f036ddbcb3409092e397b2df247bd9447e8b3f (diff) | |
download | ruby-01eba908adcd150a7b816af0dbe167c4c4912a90.tar.gz |
* lib/webrick: imported.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4130 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/webrick/log.rb')
-rw-r--r-- | lib/webrick/log.rb | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/lib/webrick/log.rb b/lib/webrick/log.rb new file mode 100644 index 0000000000..2f56102736 --- /dev/null +++ b/lib/webrick/log.rb @@ -0,0 +1,83 @@ +# +# 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 + class BasicLog + # log-level constant + FATAL, ERROR, WARN, INFO, DEBUG = 1, 2, 3, 4, 5 + + attr_accessor :level + + 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 + + def close + @log.close if @opened + @log = nil + end + + def log(level, data) + if @log && level <= @level + @log << (data + "\n") + end + end + + def fatal(msg) log(FATAL, "FATAL " << format(msg)); end + def error(msg) log(ERROR, "ERROR " << format(msg)); end + def warn(msg) log(WARN, "WARN " << format(msg)); end + def info(msg) log(INFO, "INFO " << format(msg)); end + def debug(msg) log(DEBUG, "DEBUG " << format(msg)); end + + def fatal?; @level >= FATAL; end + def error?; @level >= ERROR; end + def warn?; @level >= WARN; end + def info?; @level >= INFO; end + def debug?; @level >= DEBUG; end + + private + + def format(arg) + str = if arg.is_a?(Exception) + "#{arg.class}: #{arg.message}\n\t" << + arg.backtrace.join("\n\t") + elsif arg.respond_to?(:to_str) + arg.to_str + else + arg.inspect + end + end + end + + class Log < BasicLog + attr_accessor :time_format + + def initialize(log_file=nil, level=nil) + super(log_file, level) + @time_format = "[%Y-%m-%d %H:%M:%S]" + end + + def log(level, data) + tmp = Time.now.strftime(@time_format) + tmp << " " << data + super(level, tmp) + end + end +end |