Skip to content

101 setting the log level

rudionrails edited this page Mar 22, 2012 · 9 revisions

Yell 101: How To Setting the Log Level

Like many other logging libraries, Yell allows you to define from which level onwards you want to write your log message.

logger = Yell.new STDOUT, :level => :info

logger.debug "This is a :debug message"
#=> nil 

logger.info "This is a :info message"
#=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 : This is a :info message"

As expected, the logger instance will write for any level equal or higher to the level of :info.

Write on Certain Levels Only

The Yell::Level parser allows you to exactly specify on which levels to log, ignoring all the others. For instance: If we want to only log at the :debug and :warn levels we simply providing an array:

logger = Yell.new STDOUT, :level => [:debug, :warn]

[:debug, :info, :warn, :error, :fatal].each do |level| 
  logger.send( level, level )
end
#=> "2012-02-29T09:30:00+01:00 [DEBUG] 65784 : debug"
#=> "2012-02-29T09:30:00+01:00 [ WARN] 65784 : warn"

Write Within Level Ranges

Additionally to writing only on specific levels, you may pass a range to the :level option:

logger = Yell.new STDOUT, :level => (:debug..:warn)

[:debug, :info, :warn, :error, :fatal].each do |level| 
  logger.send( level, level )
end
#=> "2012-02-29T09:30:00+01:00 [DEBUG] 65784 : debug"
#=> "2012-02-29T09:30:00+01:00 [ INFO] 65784 : info"
#=> "2012-02-29T09:30:00+01:00 [ WARN] 65784 : warn"