Initializing log4r once

I have a problem init’ing log4r. I have the following initialization
code for log4r:

Configurator['logpath'] = './config' # was './logs'
Configurator.load_xml_file('config/log4r_config.xml')

I put it in appliction.rb, but then ofcourse at every request the logger
system will be reinitialized. This causes the logfiles to be cleared at
each request. which is not very usefull. Does anyone have an idea as how
to init log4r once, at the beginning of the application run? Help would
be very welcome.
Thanks in advance.

Fino

Fino Fontana wrote:

I have a problem init’ing log4r. I have the following initialization
code for log4r:

Configurator[‘logpath’] = ‘./config’ # was ‘./logs’
Configurator.load_xml_file(‘config/log4r_config.xml’)

I put it in appliction.rb, but then ofcourse at every request the logger
system will be reinitialized. This causes the logfiles to be cleared at
each request. which is not very usefull. Does anyone have an idea as how
to init log4r once, at the beginning of the application run? Help would
be very welcome.
Thanks in advance.

Fino

OK, I’ll answer my own question, fine with me.
An ApplicationController object (from application.rb) gets class loaded
and instantiated at every request to the server, at least in development
mode. So all instance vars and class vars get thrown away and a new
ApplicationController object is created. So the log initialization code
above is run again at every request if it’s in application.rb.

Solution is to put the code in environment.rb. This code seems to be run
only once at application start up.

So I created a LogInitializer class

class LogInitializer

require 'log4r'
require 'log4r/configurator'
include Log4r

def init()
    puts "==>LogInitializer: Starting initialization of loggers"
    Configurator['logpath'] = './config' # was './logs'
    puts ("==>LogInitializer: Loading xml config")
    Configurator.load_xml_file('config/log4r_config.xml')
    puts ("==>LogInitializer: Xml config Loaded")
    puts "==>LogInitializer: Loggers initialized"
end

end

and I call it from environment.rb:

require 'log_initializer'
li = LogInitializer.new
li.init()