-
Notifications
You must be signed in to change notification settings - Fork 21
101 different adapters for different log levels
Using multiple adapters with different log levels utilized the full potential of what Yell can do for you.
A very common use case is to log notice messages into a general file, whereas exceptions and stack traces go to a designated error.log. The following example defines a Yell logger writing debug
, info
and warn
messages into the 'application.log' and error
and warn
messages into the 'error.log'.
logger = Yell.new do |l|
l.adapter :file, 'application.log', :level => (:debug..:warn)
l.adapter :file, 'error.log', :level => :error
end
[ :debug, :info, :warn, :error, :fatal ].each do |level|
logger.send level, "This is a #{level} message"
end
# === application.log ===
#=> This is a debug message
#=> This is a info message
#=> This is a warn message
# === error.log ===
#=> This is a error message
#=> This is a fatal message
You may combine adapters with levels as you like. For instance: one file for every level:
logger = Yell.new do |l|
l.adapter :file, 'debug.log', :level => [:debug]
l.adapter :file, 'info.log', :level => [:info]
l.adapter :file, 'warn.log', :level => [:warn]
l.adapter :file, 'error.log', :level => [:error]
l.adapter :file, 'fatal.log', :level => [:fatal]
end
Most times in a production environment, you don't care about :debug
messages at all. Your logger is set to a minimum level of :info
to begin with. Also, you want your error/critical messages to go into a separate error.log file. Also, since you don't want to end up with one big and bloated logfile and need to know roughly when something critical has been logged, you want a daily rollover of your logfiles. Here is an easy Example to achive that:
logger = Yell.new do |l|
l.level = :info # will only pass :info and above to the adapters
l.adapter :datefile, 'production.log', :level => Yell.level.lte(:warn)
l.adapter :datefile, 'error.log', :level => Yell.level.gte(:error)
end