Logger in my classes


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

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

def log_it(message)

seems to work:

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

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

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


Best Regards

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs