module Pyer
This Logger
class provides a simple logging utility for Ruby applications. Log messages are sent to stdout, stderr, a file or a string by a Logger
object.
The messages will have varying levels reflecting their varying importance. The levels, and their meanings, are:
-
DEBUG : low-level information for developers
-
INFO : generic (useful) information about system operation
-
WARN : a warning
-
ERROR : an error condition
So each message has a level, and the Logger
itself has a level, which acts as a filter, so you can control the amount of information emitted from the logger without having to remove actual messages.
How to create a logger ?¶ ↑
-
Create a logger which logs messages to STDERR/STDOUT.
log = Logger.new(STDOUT, self.class) log = Logger.new(STDERR, self.class)
-
Create a logger for the file which has the specified name.
log = Logger.new('logfile.log', self.class)
-
Create a logger which logs messages to a string.
log = Logger.new(STRING, self.class)
Notice that self.class argument prints the class name of the caller object.
How to log a message ?¶ ↑
Notice the different methods being used to log messages of various levels.
Messages lower than log.level are not sent to output.
Ranking: DEBUG < INFO < WARN < ERROR
Default log.level is DEBUG. That means all messages are emitted.
-
Debug message
log.debug "dev info"
-
Information
log.info "some informations"
-
Warning message
log.warn "a warning message"
-
Error message
log.error "error is #{ @code }"
Messages are provided in a string or in a block, or both.
-
Message in block.
log.error { "Argument 'foo' not given." }
-
Message as a string.
log.error "Argument #{ @foo } mismatch."
-
Both arguments
log.error("Argument ") { "#{ @foo } mismatch." }
How to set severity level ?¶ ↑
log.level = INFO
How to close a logger ?¶ ↑
log.close
Installation¶ ↑
gem install pyer-options