A generic logging class
Fatal log level which indicates a server crash
Error log level which indicates a recoverable error
Information log level which indicates possibly useful information
Debugging error level for messages used in server development or debugging
log-level, messages above this level will be logged
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 50
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
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 84
def <<(obj)
log(INFO, obj.to_s)
end
Synonym for log(INFO
, obj.to_s)
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 66
def close
@log.close if @opened
@log = nil
end
Closes the logger (also closes the log device associated to the logger)
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 97
def debug(msg) log(DEBUG, "DEBUG " << format(msg)); end
Shortcut for logging a DEBUG
message
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 108
def debug?; @level >= DEBUG; end
Will the logger output DEBUG
messages?
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 91
def error(msg) log(ERROR, "ERROR " << format(msg)); end
Shortcut for logging an ERROR
message
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 102
def error?; @level >= ERROR; end
Will the logger output ERROR
messages?
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 89
def fatal(msg) log(FATAL, "FATAL " << format(msg)); end
Shortcut for logging a FATAL
message
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 100
def fatal?; @level >= FATAL; end
Will the logger output FATAL
messages?
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 119
def format(arg)
if arg.is_a?(Exception)
"#{arg.class}: #{AccessLog.escape(arg.message)}\n\t" <<
arg.backtrace.join("\n\t") << "\n"
elsif arg.respond_to?(:to_str)
AccessLog.escape(arg.to_str)
else
arg.inspect
end
end
Formats arg
for the logger
-
If
arg
is anException
, 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.
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 95
def info(msg) log(INFO, "INFO " << format(msg)); end
Shortcut for logging an INFO
message
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 106
def info?; @level >= INFO; end
Will the logger output INFO
messages?
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 75
def log(level, data)
if @log && level <= @level
data += "\n" if /\n\Z/ !~ data
@log << data
end
end
Logs data
at level
if the given level is above the current log level.
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 93
def warn(msg) log(WARN, "WARN " << format(msg)); end
Shortcut for logging a WARN
message
# File tmp/rubies/ruby-2.4.10/lib/webrick/log.rb, line 104
def warn?; @level >= WARN; end
Will the logger output WARN
messages?