Bug #6738 » lib_logger_rdoc_typos.patch
lib/logger.rb (Arbeitskopie) | ||
---|---|---|
#
|
||
# The messages have associated levels, such as +INFO+ or +ERROR+ that indicate
|
||
# their importance. You can then give the Logger a level, and only messages
|
||
# at that level of higher will be printed.
|
||
# at that level or higher will be printed.
|
||
#
|
||
# The levels are:
|
||
#
|
||
# +FATAL+:: an unhandleable error that results in a program crash
|
||
# +ERROR+:: a handleable error condition
|
||
# +WARN+:: a warning
|
||
# +INFO+:: generic (useful) information about system operation
|
||
# +DEBUG+:: low-level information for developers
|
||
# +UNKNOWN+:: An unknown message that should always be logged.
|
||
# +FATAL+:: An unhandleable error that results in a program crash.
|
||
# +ERROR+:: A handleable error condition.
|
||
# +WARN+:: A warning.
|
||
# +INFO+:: Generic (useful) information about system operation.
|
||
# +DEBUG+:: Low-level information for developers.
|
||
#
|
||
# For instance, in a production system, you may have your Logger set to
|
||
# +INFO+ or even +WARN+
|
||
# +INFO+ or even +WARN+.
|
||
# When you are developing the system, however, you probably
|
||
# want to know about the program's internal state, and would set the Logger to
|
||
# +DEBUG+.
|
||
... | ... | |
#
|
||
# === Example
|
||
#
|
||
# This creates a logger to the standard output stream, with a level of +WARN+
|
||
# This creates a Logger to the standard output stream, with a level of +WARN+:
|
||
#
|
||
# log = Logger.new(STDOUT)
|
||
# log.level = Logger::WARN
|
||
#
|
||
# log.debug("Created logger")
|
||
# log.info("Program started")
|
||
# log.warn("Nothing to do!")
|
||
# log.debug('Created logger')
|
||
# log.info('Program started')
|
||
# log.warn('Nothing to do!')
|
||
#
|
||
# begin
|
||
# File.each_line(path) do |line|
|
||
... | ... | |
# end
|
||
# end
|
||
# rescue => err
|
||
# log.fatal("Caught exception; exiting")
|
||
# log.fatal('Caught exception; exiting')
|
||
# log.fatal(err)
|
||
# end
|
||
#
|
||
... | ... | |
#
|
||
# file = File.open('foo.log', File::WRONLY | File::APPEND)
|
||
# # To create new (and to remove old) logfile, add File::CREAT like;
|
||
# # file = open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
|
||
# # file = File.open('foo.log', File::WRONLY | File::APPEND | File::CREAT)
|
||
# logger = Logger.new(file)
|
||
#
|
||
# 4. Create a logger which ages logfile once it reaches a certain size. Leave
|
||
# 10 "old log files" and each file is about 1,024,000 bytes.
|
||
# 4. Create a logger which ages the logfile once it reaches a certain size.
|
||
# Leave 10 "old" log files where each file is about 1,024,000 bytes.
|
||
#
|
||
# logger = Logger.new('foo.log', 10, 1024000)
|
||
#
|
||
# 5. Create a logger which ages logfile daily/weekly/monthly.
|
||
# 5. Create a logger which ages the logfile daily/weekly/monthly.
|
||
#
|
||
# logger = Logger.new('foo.log', 'daily')
|
||
# logger = Logger.new('foo.log', 'weekly')
|
||
... | ... | |
# +debug+. +add+ is used below to log a message of an arbitrary (perhaps
|
||
# dynamic) level.
|
||
#
|
||
# 1. Message in block.
|
||
# 1. Message in a block.
|
||
#
|
||
# logger.fatal { "Argument 'foo' not given." }
|
||
#
|
||
# 2. Message as a string.
|
||
#
|
||
# logger.error "Argument #{ @foo } mismatch."
|
||
# logger.error "Argument #{@foo} mismatch."
|
||
#
|
||
# 3. With progname.
|
||
#
|
||
# logger.info('initialize') { "Initializing..." }
|
||
# logger.info('initialize') { 'Initializing...' }
|
||
#
|
||
# 4. With severity.
|
||
#
|
||
... | ... | |
# but to delay their evaluation until and unless the message is
|
||
# logged. For example, if we have the following:
|
||
#
|
||
# logger.debug { "This is a " + potentially + " expensive operation" }
|
||
# logger.debug { 'This is a ' + potentially + ' expensive operation' }
|
||
#
|
||
# If the logger's level is +INFO+ or higher, no debug messages will be logged,
|
||
# and the entire block will not even be evaluated. Compare to this:
|
||
#
|
||
# logger.debug("This is a " + potentially + " expensive operation")
|
||
# logger.debug('This is a ' + potentially + ' expensive operation')
|
||
#
|
||
# Here, the string concatenation is done every time, even if the log
|
||
# level is not set to show the debug message.
|
||
... | ... | |
#
|
||
# logger.level = Logger::INFO
|
||
#
|
||
# DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
|
||
# # DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
|
||
#
|
||
#
|
||
# == Format
|
||
... | ... | |
# default. The default format and a sample are shown below:
|
||
#
|
||
# Log format:
|
||
# SeverityID, [Date Time mSec #pid] SeverityLabel -- ProgName: message
|
||
# SeverityID, [Date Time uSec #pid] SeverityLabel -- ProgName: message
|
||
#
|
||
# Log sample:
|
||
# I, [Wed Mar 03 02:34:24 JST 1999 895701 #19074] INFO -- Main: info.
|
||
#
|
||
# You may change the date and time format via #datetime_format=
|
||
# You may change the date and time format via #datetime_format=.
|
||
#
|
||
# logger.datetime_format = "%Y-%m-%d %H:%M:%S"
|
||
# logger.datetime_format = '%Y-%m-%d %H:%M:%S'
|
||
# # e.g. "2004-01-03 00:54:26"
|
||
#
|
||
# Or, you may change the overall format with #formatter= method.
|
||
# Or, you may change the overall format via the #formatter= method.
|
||
#
|
||
# logger.formatter = proc do |severity, datetime, progname, msg|
|
||
# "#{datetime}: #{msg}\n"
|
||
# end
|
||
# # e.g. "Thu Sep 22 08:51:08 GMT+9:00 2005: hello world"
|
||
# # e.g. 'Thu Sep 22 08:51:08 GMT+9:00 2005: hello world'
|
||
#
|
||
class Logger
|
||
VERSION = "1.2.7"
|
||
... | ... | |
# Logging severity.
|
||
module Severity
|
||
# Low-level information, mostly for developers
|
||
# Low-level information, mostly for developers.
|
||
DEBUG = 0
|
||
# generic, useful information about system operation
|
||
# Generic (useful) information about system operation.
|
||
INFO = 1
|
||
# a warning
|
||
# A warning.
|
||
WARN = 2
|
||
# a handleable error condition
|
||
# A handleable error condition.
|
||
ERROR = 3
|
||
# an unhandleable error that results in a program crash
|
||
# An unhandleable error that results in a program crash.
|
||
FATAL = 4
|
||
# an unknown message that should always be logged
|
||
# An unknown message that should always be logged.
|
||
UNKNOWN = 5
|
||
end
|
||
include Severity
|
||
... | ... | |
# Logging severity threshold (e.g. <tt>Logger::INFO</tt>).
|
||
attr_accessor :level
|
||
# program name to include in log messages.
|
||
# Program name to include in log messages.
|
||
attr_accessor :progname
|
||
# Set date-time format.
|
||
... | ... | |
# Logging formatter, as a +Proc+ that will take four arguments and
|
||
# return the formatted message. The arguments are:
|
||
#
|
||
# +severity+:: The Severity of the log message
|
||
# +time+:: A Time instance representing when the message was logged
|
||
# +progname+:: The #progname configured, or passed to the logger method
|
||
# +severity+:: The Severity of the log message.
|
||
# +time+:: A Time instance representing when the message was logged.
|
||
# +progname+:: The #progname configured, or passed to the logger method.
|
||
# +msg+:: The _Object_ the user passed to the log message; not necessarily a
|
||
# String.
|
||
#
|
||
... | ... | |
#
|
||
# +true+ if successful, +false+ otherwise.
|
||
#
|
||
# When the given severity is not high enough (for this particular logger), log
|
||
# no message, and return +true+.
|
||
# When the given severity is not high enough (for this particular logger),
|
||
# log no message, and return +true+.
|
||
#
|
||
# === Description
|
||
#
|
||
... | ... | |
#
|
||
# * Logfile is not locked.
|
||
# * Append open does not need to lock file.
|
||
# * If the OS which supports multi I/O, records possibly be mixed.
|
||
# * If the OS supports multi I/O, records possibly may be mixed.
|
||
#
|
||
def add(severity, message = nil, progname = nil, &block)
|
||
severity ||= UNKNOWN
|
||
... | ... | |
#
|
||
# :call-seq:
|
||
# info(message)
|
||
# info(progname,&block)
|
||
# info(progname, &block)
|
||
#
|
||
# Log an +INFO+ message.
|
||
#
|
||
# +message+:: the message to log; does not need to be a String
|
||
# +progname+:: in the block form, this is the #progname to use in the
|
||
# the log message. The default can be set with #progname=
|
||
# <tt>&block</tt>:: evaluates to the message to log. This is not evaluated
|
||
# +message+:: The message to log; does not need to be a String.
|
||
# +progname+:: In the block form, this is the #progname to use in the
|
||
# log message. The default can be set with #progname=.
|
||
# <tt>&block</tt>:: Evaluates to the message to log. This is not evaluated
|
||
# unless the logger's level is sufficient
|
||
# to log the message. This allows you to create
|
||
# potentially expensive logging messages that are
|
||
... | ... | |
#
|
||
# === Examples
|
||
#
|
||
# logger.info("MainApp") { "Received connection from #{ip}" }
|
||
# logger.info('MainApp') { "Received connection from #{ip}" }
|
||
# # ...
|
||
# logger.info "Waiting for input from user"
|
||
# logger.info 'Waiting for input from user'
|
||
# # ...
|
||
# logger.info { "User typed #{input}" }
|
||
#
|
||
... | ... | |
#
|
||
# Log an +UNKNOWN+ message. This will be printed no matter what the logger's
|
||
# level.
|
||
# level is.
|
||
#
|
||
# See #info for more information.
|
||
#
|
||
... | ... | |
private
|
||
# Severity label for logging. (max 5 char)
|
||
# Severity label for logging (max 5 chars).
|
||
SEV_LABEL = %w(DEBUG INFO WARN ERROR FATAL ANY)
|
||
def format_severity(severity)
|
||
... | ... | |
end
|
||
# Default formatter for log messages
|
||
# Default formatter for log messages.
|
||
class Formatter
|
||
Format = "%s, [%s#%d] %5s -- %s: %s\n"
|
||
... | ... | |
#
|
||
# == Description
|
||
#
|
||
# Application -- Add logging support to your application.
|
||
# Application --- Add logging support to your application.
|
||
#
|
||
# == Usage
|
||
#
|
||
# 1. Define your application class as a sub-class of this class.
|
||
# 2. Override 'run' method in your class to do many things.
|
||
# 3. Instantiate it and invoke 'start'.
|
||
# 2. Override the +run+ method in your class to do many things.
|
||
# 3. Instantiate it and invoke #start.
|
||
#
|
||
# == Example
|
||
#
|