Error using Log4r

798 views Asked by At

I have a problem using Log4r.

uninitialized constant Log4r::Logger::RootLogger

I tried this, but still got an error:

>> require "log4r"
=> true
>> Log4r::DEBUG
NameError: uninitialized constant Log4r::DEBUG
>> Log4r::Logger.root
=> uninitialized constant Log4r::Logger::RootLogger
from /var/lib/gems/1.9.1/gems/log4r-1.1.11/lib/log4r/staticlogger.rb:5:in `root'
    from (irb):5
    from /usr/bin/irb:12:in `<main>'
2

There are 2 answers

0
knut On BEST ANSWER

Your problem with Log4r::Logger.root is version depending (the actual version 1.1.11 has this problem).

You may use the previous log4r-version 1.1.10:

gem 'log4r', '<=1.1.10' #or '= 1.1.10'
require 'log4r'
Log4r::Logger.root

log4r defines the constants like Log4r::DEBUG with the creation of the first logger.

You need a Log4r::Logger.new('dummy') before you have access to the level constants.

require 'log4r'

p defined? Log4r::INFO   #false
Log4r::Logger.new('dummy')
p defined? Log4r::INFO   #constant -> is defined

Some background: There is a constant Log4r::Log4rConfig::LogLevels defining the different levels. The level-constants are defined, when the first logger is created. You may define them also with Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)

This technique allows it to create loggers with different logging levels.

1
Peter Alfvin On

AlthoughLog4r::Logger.root no longer enables the constants, the code included later on in the answer you referred to does introduce the constants:

Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)

per the following:

MacbookAir1:so1 palfvin$ irb
2.0.0p247 :001 > require 'log4r'
 => true 
2.0.0p247 :002 > Log4r.define_levels(*Log4r::Log4rConfig::LogLevels)
 => 5 
2.0.0p247 :003 > Log4r::DEBUG
 => 1 
2.0.0p247 :004 >