Class: WEBrick::BasicLog

Inherits:
Object
  • Object
show all
Defined in:
lib/webrick/log.rb

Overview

A generic logging class

Direct Known Subclasses

Log

Constant Summary collapse

FATAL =

Fatal log level which indicates a server crash

1
ERROR =

Error log level which indicates a recoverable error

2
WARN =

Warning log level which indicates a possible problem

3
INFO =

Information log level which indicates possibly useful information

4
DEBUG =

Debugging error level for messages used in server development or debugging

5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(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



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/webrick/log.rb', line 50

def initialize(log_file=nil, level=nil)
  @level = level || INFO
  case log_file
  when String
    @log = File.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

#levelObject

log-level, messages above this level will be logged



41
42
43
# File 'lib/webrick/log.rb', line 41

def level
  @level
end

Instance Method Details

#<<(obj) ⇒ Object

Synonym for log(INFO, obj.to_s)



84
85
86
# File 'lib/webrick/log.rb', line 84

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

#closeObject

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



66
67
68
69
# File 'lib/webrick/log.rb', line 66

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

#debug(msg) ⇒ Object

Shortcut for logging a DEBUG message



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

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

#debug?Boolean

Will the logger output DEBUG messages?

Returns:

  • (Boolean)


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

def debug?; @level >= DEBUG; end

#error(msg) ⇒ Object

Shortcut for logging an ERROR message



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

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

#error?Boolean

Will the logger output ERROR messages?

Returns:

  • (Boolean)


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

def error?; @level >= ERROR; end

#fatal(msg) ⇒ Object

Shortcut for logging a FATAL message



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

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

#fatal?Boolean

Will the logger output FATAL messages?

Returns:

  • (Boolean)


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

def fatal?; @level >= FATAL; end

#info(msg) ⇒ Object

Shortcut for logging an INFO message



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

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

#info?Boolean

Will the logger output INFO messages?

Returns:

  • (Boolean)


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

def info?;  @level >= INFO; end

#log(level, data) ⇒ Object

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



75
76
77
78
79
80
# File 'lib/webrick/log.rb', line 75

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

#warn(msg) ⇒ Object

Shortcut for logging a WARN message



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

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

#warn?Boolean

Will the logger output WARN messages?

Returns:

  • (Boolean)


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

def warn?;  @level >= WARN; end