Logger in my classes

Hi,

I wrote a class which is not an AR extension, nor a controller.
I’d like to use logger.* methods there too.

My class is in lib/ dir. I include it in environment.rb with the line
require ‘lib/localization’

just after the line
require File.join(File.dirname(FILE), ‘boot’)

I tried many ways to reuse the logger class. The cleaner way seemed to
be based on Object::RAILS_DEFAULT_LOGGER that is defined in

/railties/lib/initializer.rb:
silence_warnings { Object.const_set “RAILS_DEFAULT_LOGGER”, logger }

But all I get is an uninitialized constant :frowning:

Please tell me the right way to reuse logger.

Hi, Jean-Christophe,

never been in your situation but I’ve made a short test anyway

require ‘logger’

class TestIt

attr_reader :logger
attr_writer :logger

def initialize
@logger = RAILS_DEFAULT_LOGGER
end

def log_it(message)
@logger.info(message)
end
end

seems to work:

F:\home\jan\workspace\blauton>ruby script/console
Loading development environment.

test = TestIt.new
=> #<TestIt:0x34d2b18 @logger=#<Logger:0x35ba358 @level=0,
@datetime_format=nil,
@progname=nil, @logdev=#<Logger::LogDevice:0x35ba310
@shift_size=1048576, @shif
t_age=0,
@filename="./script/…/config/…/config/…/log/development.log", @dev=#
<File:./script/…/config/…/config/…/log/development.log>>>>

test.log_it(‘Hello Jean-Christophe’)
=> true

exit

Best Regards
Jan