123456789_123456789_123456789_123456789_123456789_

Class: WEBrick::BasicLog

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Log
Inherits: Object
Defined in: lib/webrick/log.rb

Overview

A generic logging class

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(log_file = nil, level = nil) ⇒ BasicLog

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

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 49

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

Instance Attribute Details

#debug?Boolean (readonly)

Will the logger output DEBUG messages?

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 107

def debug?; @level >= DEBUG; end

#error?Boolean (readonly)

Will the logger output ERROR messages?

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 101

def error?; @level >= ERROR; end

#fatal?Boolean (readonly)

Will the logger output FATAL messages?

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 99

def fatal?; @level >= FATAL; end

#info?Boolean (readonly)

Will the logger output INFO messages?

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 105

def info?;  @level >= INFO; end

#level (rw)

log-level, messages above this level will be logged

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 40

attr_accessor :level

#warn?Boolean (readonly)

Will the logger output WARN messages?

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 103

def warn?;  @level >= WARN; end

Instance Method Details

#<<(obj)

Synonym for log(INFO, obj.to_s)

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 83

def <<(obj)
  log(INFO, obj.to_s)
end

#close

Closes the logger (also closes the log device associated to the logger)

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 65

def close
  @log.close if @opened
  @log = nil
end

#debug(msg) (readonly)

Shortcut for logging a DEBUG message

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 96

def debug(msg) log(DEBUG, "DEBUG " << format(msg)); end

#error(msg) (readonly)

Shortcut for logging an ERROR message

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 90

def error(msg) log(ERROR, "ERROR " << format(msg)); end

#fatal(msg) (readonly)

Shortcut for logging a FATAL message

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 88

def fatal(msg) log(FATAL, "FATAL " << format(msg)); end

#format(arg) (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.

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 118

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

#info(msg) (readonly)

Shortcut for logging an INFO message

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 94

def info(msg)  log(INFO,  "INFO  " << format(msg)); end

#log(level, data)

Logs data at #level if the given level is above the current log level.

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 74

def log(level, data)
  if @log && level <= @level
    data += "\n" if /\n\Z/ !~ data
    @log << data
  end
end

#warn(msg) (readonly)

Shortcut for logging a WARN message

[ GitHub ]

  
# File 'lib/webrick/log.rb', line 92

def warn(msg)  log(WARN,  "WARN  " << format(msg)); end