Class: Logger
Defined Under Namespace
Modules: Severity Classes: Application, Error, Formatter, LogDevice, ShiftingError
Constant Summary collapse
- VERSION =
"1.2.6"- ProgName =
"#{name.chomp(",v")}/#{rev}"
Constants included from Severity
Severity::DEBUG, Severity::ERROR, Severity::FATAL, Severity::INFO, Severity::UNKNOWN, Severity::WARN
Instance Attribute Summary collapse
-
#formatter ⇒ Object
Logging formatter.
-
#level ⇒ Object
(also: #sev_threshold)
Logging severity threshold (e.g.
Logger::INFO). -
#progname ⇒ Object
Logging program name.
Instance Method Summary collapse
-
#<<(msg) ⇒ Object
Dump given message to the log device without any formatting.
-
#add(severity, message = nil, progname = nil, &block) ⇒ Object
(also: #log)
Synopsis.
-
#close ⇒ Object
Close the logging device.
- #datetime_format ⇒ Object
-
#datetime_format=(datetime_format) ⇒ Object
Logging date-time format (string passed to
strftime). -
#debug(progname = nil, &block) ⇒ Object
Log a
DEBUGmessage. -
#debug? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofDEBUGmessages. -
#error(progname = nil, &block) ⇒ Object
Log an
ERRORmessage. -
#error? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofERRORmessages. -
#fatal(progname = nil, &block) ⇒ Object
Log a
FATALmessage. -
#fatal? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofFATALmessages. -
#info(progname = nil, &block) ⇒ Object
Log an
INFOmessage. -
#info? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofINFOmessages. -
#initialize(logdev, shift_age = 0, shift_size = 1048576) ⇒ Logger
constructor
Synopsis.
-
#unknown(progname = nil, &block) ⇒ Object
Log an
UNKNOWNmessage. -
#warn(progname = nil, &block) ⇒ Object
Log a
WARNmessage. -
#warn? ⇒ Boolean
Returns
trueiff the current severity level allows for the printing ofWARNmessages.
Constructor Details
#initialize(logdev, shift_age = 0, shift_size = 1048576) ⇒ Logger
Synopsis
Logger.new(name, shift_age = 7, shift_size = 1048576)
Logger.new(name, shift_age = 'weekly')
Args
logdev-
The log device. This is a filename (String) or IO object (typically
STDOUT,STDERR, or an open file). shift_age-
Number of old log files to keep, or frequency of rotation (
daily,weeklyormonthly). shift_size-
Maximum logfile size (only applies when
shift_ageis a number).
Description
Create an instance.
256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/rubysl/logger/logger.rb', line 256 def initialize(logdev, shift_age = 0, shift_size = 1048576) @progname = nil @level = DEBUG @default_formatter = Formatter.new @formatter = nil @logdev = nil if logdev @logdev = LogDevice.new(logdev, :shift_age => shift_age, :shift_size => shift_size) end end |
Instance Attribute Details
#formatter ⇒ Object
Logging formatter. formatter#call is invoked with 4 arguments; severity, time, progname and msg for each log. Bear in mind that time is a Time and msg is an Object that user passed and it could not be a String. It is expected to return a logdev#write-able Object. Default formatter is used when no formatter is set.
210 211 212 |
# File 'lib/rubysl/logger/logger.rb', line 210 def formatter @formatter end |
#level ⇒ Object Also known as: sev_threshold
Logging severity threshold (e.g. Logger::INFO).
191 192 193 |
# File 'lib/rubysl/logger/logger.rb', line 191 def level @level end |
#progname ⇒ Object
Logging program name.
194 195 196 |
# File 'lib/rubysl/logger/logger.rb', line 194 def progname @progname end |
Instance Method Details
#<<(msg) ⇒ Object
Dump given message to the log device without any formatting. If no log device exists, return nil.
336 337 338 339 340 |
# File 'lib/rubysl/logger/logger.rb', line 336 def <<(msg) unless @logdev.nil? @logdev.write(msg) end end |
#add(severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log
Synopsis
Logger#add(severity, message = nil, progname = nil) { ... }
Args
severity-
Severity. Constants are defined in Logger namespace:
DEBUG,INFO,WARN,ERROR,FATAL, orUNKNOWN. message-
The log message. A String or Exception.
progname-
Program name string. Can be omitted. Treated as a message if no
messageandblockare given. block-
Can be omitted. Called to get a message string if
messageis nil.
Return
true if successful, false otherwise.
When the given severity is not high enough (for this particular logger), log no message, and return true.
Description
Log a message if the given severity is high enough. This is the generic logging method. Users will be more inclined to use #debug, #info, #warn, #error, and #fatal.
Message format: message can be any object, but it has to be converted to a String in order to log it. Generally, inspect is used if the given object is not a String. A special case is an Exception object, which will be printed in detail, including message, class, and backtrace. See #msg2str for the implementation if required.
Bugs
-
Logfile is not locked.
-
Append open does not need to lock file.
-
But on the OS which supports multi I/O, records possibly be mixed.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/rubysl/logger/logger.rb', line 312 def add(severity, = nil, progname = nil, &block) severity ||= UNKNOWN if @logdev.nil? or severity < @level return true end progname ||= @progname if .nil? if block_given? = yield else = progname progname = @progname end end @logdev.write( (format_severity(severity), Time.now, progname, )) true end |
#close ⇒ Object
Close the logging device.
417 418 419 |
# File 'lib/rubysl/logger/logger.rb', line 417 def close @logdev.close if @logdev end |
#datetime_format ⇒ Object
201 202 203 |
# File 'lib/rubysl/logger/logger.rb', line 201 def datetime_format @default_formatter.datetime_format end |
#datetime_format=(datetime_format) ⇒ Object
Logging date-time format (string passed to strftime).
197 198 199 |
# File 'lib/rubysl/logger/logger.rb', line 197 def datetime_format=(datetime_format) @default_formatter.datetime_format = datetime_format end |
#debug(progname = nil, &block) ⇒ Object
Log a DEBUG message.
See #info for more information.
347 348 349 |
# File 'lib/rubysl/logger/logger.rb', line 347 def debug(progname = nil, &block) add(DEBUG, nil, progname, &block) end |
#debug? ⇒ Boolean
Returns true iff the current severity level allows for the printing of DEBUG messages.
217 |
# File 'lib/rubysl/logger/logger.rb', line 217 def debug?; @level <= DEBUG; end |
#error(progname = nil, &block) ⇒ Object
Log an ERROR message.
See #info for more information.
391 392 393 |
# File 'lib/rubysl/logger/logger.rb', line 391 def error(progname = nil, &block) add(ERROR, nil, progname, &block) end |
#error? ⇒ Boolean
Returns true iff the current severity level allows for the printing of ERROR messages.
229 |
# File 'lib/rubysl/logger/logger.rb', line 229 def error?; @level <= ERROR; end |
#fatal(progname = nil, &block) ⇒ Object
Log a FATAL message.
See #info for more information.
400 401 402 |
# File 'lib/rubysl/logger/logger.rb', line 400 def fatal(progname = nil, &block) add(FATAL, nil, progname, &block) end |
#fatal? ⇒ Boolean
Returns true iff the current severity level allows for the printing of FATAL messages.
233 |
# File 'lib/rubysl/logger/logger.rb', line 233 def fatal?; @level <= FATAL; end |
#info(progname = nil, &block) ⇒ Object
Log an INFO message.
The message can come either from the progname argument or the block. If both are provided, then the block is used as the message, and progname is used as the program name.
Examples
logger.info("MainApp") { "Received connection from #{ip}" }
# ...
logger.info "Waiting for input from user"
# ...
logger.info { "User typed #{input}" }
You’ll probably stick to the second form above, unless you want to provide a program name (which you can do with Logger#progname= as well).
Return
See #add.
373 374 375 |
# File 'lib/rubysl/logger/logger.rb', line 373 def info(progname = nil, &block) add(INFO, nil, progname, &block) end |
#info? ⇒ Boolean
Returns true iff the current severity level allows for the printing of INFO messages.
221 |
# File 'lib/rubysl/logger/logger.rb', line 221 def info?; @level <= INFO; end |
#unknown(progname = nil, &block) ⇒ Object
Log an UNKNOWN message. This will be printed no matter what the logger level.
See #info for more information.
410 411 412 |
# File 'lib/rubysl/logger/logger.rb', line 410 def unknown(progname = nil, &block) add(UNKNOWN, nil, progname, &block) end |
#warn(progname = nil, &block) ⇒ Object
Log a WARN message.
See #info for more information.
382 383 384 |
# File 'lib/rubysl/logger/logger.rb', line 382 def warn(progname = nil, &block) add(WARN, nil, progname, &block) end |
#warn? ⇒ Boolean
Returns true iff the current severity level allows for the printing of WARN messages.
225 |
# File 'lib/rubysl/logger/logger.rb', line 225 def warn?; @level <= WARN; end |