Skip to content
This repository

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"

NOTE: Using a range like (:info..:warn) may only be possible with ruby 1.9.x. If that is the case, you are still able to use a numeric range, e.g. (1..3).

Pass a Yell::Level instance

In order to set your level, you can also pass a Yell::Level instance like so:

# DEFAULT
logger = Yell.new :level => Yell.level(:error) # grater than or equal to :error

# GREATER THAN
logger = Yell.new :level => Yell.level.gte(:error ) # greater than or equal to :error
logger = Yell.new :level => Yell.level.gt(:error )  # greater than :error

# LOWER THAN
logger = Yell.new :level => Yell.level.lte(:error ) # lower than or equal to :error
logger = Yell.new :level => Yell.level.lt(:error )  # lower than :error

# ONLY AT
logger = Yell.new :level => Yell.level.at(:error)       # only at :error
logger = Yell.new :level => Yell.level.at(:info. :warn) # only at :info and :warn

Of couse, you can combine those:

# BETWEEN
logger = Yell.new :level => Yell.level.gte(:info).lt(:fatal) # :info, :warn, :error

# GREATER THAT AND AT COMBINED
logger = Yell.new :level => Tell.level.gte(:error).at(:debug) # :debug, :error, :fatal

Pass a string of levels

In order to set your level, you can also pass a Yell::Level instance like so:

# DEFAULT
logger = Yell.new :level => 'error' # grater than or equal to :error

# GREATER THAN
logger = Yell.new :level => 'gte.error' # greater than or equal to :error
logger = Yell.new :level => 'gt.error'  # greater than :error

# LOWER THAN
logger = Yell.new :level => 'lte.error' # lower than or equal to :error
logger = Yell.new :level => 'lt.error'  # lower than :error

# ONLY AT
logger = Yell.new :level => 'at.error'        # only at :error
logger = Yell.new :level => 'at.info at.warn' # only at :info and :warn

Of couse, you can combine those, too:

# BETWEEN
logger = Yell.new :level => 'gte.info lt.fatal' # :info, :warn, :error

# GREATER THAT AND AT COMBINED
logger = Yell.new :level => 'gte.error at.debug' # :debug, :error, :fatal
Something went wrong with that request. Please try again.