TwP / logging

A flexible logging library for use in Ruby programs based on the design of Java's log4j library.
https://rubygems.org/gems/logging
MIT License
530 stars 101 forks source link

Possibility to set log_level for each appender? #168

Closed ErikAGriffin closed 7 years ago

ErikAGriffin commented 7 years ago

I would like to set different log_levels based on the different appenders in my root logger, is this possible? How to do so is not immediately obvious

For example, I have two appenders in my root logger, stdout and rolling_file. I would like the log level for stdout to be :info, and the level for the rolling file to be :debug

TwP commented 7 years ago

The default behavior is for an appender to output all log events sent to it. So your rolling_file appender is already configured appropriately to output :debug level events.

You can pass a :level setting to the appender when it is first created, or you can set the level at any time using the level= accessor. Here is an example for the stdout appender.

require 'logging'

# initially set to log warnings and above
Logging.appenders.stdout("my_stdout", level: :warn)

# now we grab our appender instance by name and change the log level
my_stdout = Logging.appenders["my_stdout"]
my_stdout.level = :info

You do not have to create a custom stdout appender. You can just as easily change the level for the regular stdout appender.

Logging.appenders.stdout.level = :info

Remember, your loggers will need to be configured to output debug messages.