| Class | Logger |
| In: |
lib/logger.rb
|
| Parent: | Object |
| VERSION | = | "1.2.6" | ||
| ProgName | = | "#{$1}/#{$2}" | ||
| SEV_LABEL | = | %w(DEBUG INFO WARN ERROR FATAL ANY) | Severity label for logging. (max 5 char) |
| level | -> | sev_threshold |
| level= | -> | sev_threshold= |
| formatter | [RW] | Logging formatter. formattercall 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 logdevwrite-able Object. Default formatter is used when no formatter is set. |
| level | [RW] | Logging severity threshold (e.g. Logger::INFO). |
| progname | [RW] | Logging program name. |
Logger.new(name, shift_age = 7, shift_size = 1048576) Logger.new(name, shift_age = 'weekly')
| 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, weekly or monthly). |
| shift_size: | Maximum logfile size (only applies when shift_age is a number). |
Create an instance.
# File lib/logger.rb, line 265
265: def initialize(logdev, shift_age = 0, shift_size = 1048576)
266: @progname = nil
267: @level = DEBUG
268: @default_formatter = Formatter.new
269: @formatter = nil
270: @logdev = nil
271: if logdev
272: @logdev = LogDevice.new(logdev, :shift_age => shift_age,
273: :shift_size => shift_size)
274: end
275: end
Dump given message to the log device without any formatting. If no log device exists, return nil.
# File lib/logger.rb, line 345
345: def <<(msg)
346: unless @logdev.nil?
347: @logdev.write(msg)
348: end
349: end
Logger#add(severity, message = nil, progname = nil) { ... }
| severity: | Severity. Constants are defined in Logger namespace: DEBUG, INFO, WARN, ERROR, FATAL, or UNKNOWN. |
| message: | The log message. A String or Exception. |
| progname: | Program name string. Can be omitted. Treated as a message if no message and block are given. |
| block: | Can be omitted. Called to get a message string if message is nil. |
true if successful, false otherwise.
When the given severity is not high enough (for this particular logger), log no message, and return true.
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.
# File lib/logger.rb, line 321
321: def add(severity, message = nil, progname = nil, &block)
322: severity ||= UNKNOWN
323: if @logdev.nil? or severity < @level
324: return true
325: end
326: progname ||= @progname
327: if message.nil?
328: if block_given?
329: message = yield
330: else
331: message = progname
332: progname = @progname
333: end
334: end
335: @logdev.write(
336: format_message(format_severity(severity), Time.now, progname, message))
337: true
338: end
Close the logging device.
# File lib/logger.rb, line 426
426: def close
427: @logdev.close if @logdev
428: end
# File lib/logger.rb, line 210
210: def datetime_format
211: @default_formatter.datetime_format
212: end
Logging date-time format (string passed to strftime).
# File lib/logger.rb, line 206
206: def datetime_format=(datetime_format)
207: @default_formatter.datetime_format = datetime_format
208: end
Returns true iff the current severity level allows for the printing of DEBUG messages.
# File lib/logger.rb, line 226
226: def debug?; @level <= DEBUG; end
Returns true iff the current severity level allows for the printing of ERROR messages.
# File lib/logger.rb, line 238
238: def error?; @level <= ERROR; end
Returns true iff the current severity level allows for the printing of FATAL messages.
# File lib/logger.rb, line 242
242: def fatal?; @level <= FATAL; end
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.
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).
See add.
# File lib/logger.rb, line 382
382: def info(progname = nil, &block)
383: add(INFO, nil, progname, &block)
384: end
Returns true iff the current severity level allows for the printing of INFO messages.
# File lib/logger.rb, line 230
230: def info?; @level <= INFO; end
Returns true iff the current severity level allows for the printing of WARN messages.
# File lib/logger.rb, line 234
234: def warn?; @level <= WARN; end
# File lib/logger.rb, line 439
439: def format_message(severity, datetime, progname, msg)
440: (@formatter || @default_formatter).call(severity, datetime, progname, msg)
441: end