ruby-on-railsrubylogginglog4r

Error using Log4r


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>'

Solution

  • 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.