Forum: Ruby on Rails logger in my classes

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Jean-Christophe M. (Guest)
on 2006-01-11 20:39
(Received via mailing list)

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 :(

Please tell me the right way to reuse logger.
Jan P. (Guest)
on 2006-01-11 22:48
(Received via mailing list)
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: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
 >> exit

Best Regards
This topic is locked and can not be replied to.