I can define my own log-levels with log4r:
require 'log4r'
require 'log4r/configurator'
# This is how we specify our levels
Log4r::Configurator.custom_levels "Foo", "Bar", "Baz"
log = Log4r::Logger.new('custom levels')
p log.levels #-> ["ALL", "Foo", "Bar", "Baz", "OFF"]
log.add Log4r::StdoutOutputter.new('console')
puts log.foo? #-> true
log.foo "This is foo 1"
log.level = Log4r::Bar
puts log.foo? #->false
log.foo "This is foo 2"
The log4r-documentation says:
Also, custom levels should be set before anything else is done with Log4r, otherwise the default levels will be loaded.
This restriction is the source of two problems:
- Setting new levels with
Log4r::Configurator.custom_levels
works only, if there is no logger defined. - If I create another logger after defining my own levels, I get again the custom levels.
Problem 1:
Example:
require 'log4r'
require 'log4r/configurator'
#Create a dummy logger
Log4r::Logger.new('dummy')
# Define new levels -> does not work after creation of another logger.
Log4r::Configurator.custom_levels "Foo", "Bar", "Baz"
log = Log4r::Logger.new('custom levels')
p log.levels #-> ["ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "OFF"]
The custom levels are not used.
Problem 2
If I define my own levels, I have no possibility to go back to the defaults.
Example:
require 'log4r'
require 'log4r/configurator'
# Define customer levels
Log4r::Configurator.custom_levels "Foo", "Bar", "Baz"
Log4r::Logger.new('dummy with custom levels')
#Reset to standard levels ["DEBUG", "INFO", "WARN", "ERROR", "FATAL"] <- this does not work
Log4r::Configurator.custom_levels( *Log4r::Log4rConfig::LogLevels )
log2 = Log4r::Logger.new('log')
p log2.levels #-> ["ALL", "Foo", "Bar", "Baz", "OFF"], but I wanted ["DEBUG", "INFO", "WARN", "ERROR", "FATAL"]
Background:
I want to create a logger with my own levels similar to Apache:
DEBUG INFO NOTICE WARN ERROR CRIT ALERT EMERG
.
When I do so, I get two problems:
- If another application uses also log4r, that application has no fatal-level. This can be solved, if I add a fatal-level in my application.
- If another application is loaded before my application and creates a logger, my application will fail, because my customer levels are not available.
My question:
How can I create a logger with customized log levels and without side effects to other loggers?