Forum: Ruby on Rails logger in my classes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eb96620826312516bb4a2d6deeee6688?d=identicon&s=25 Jean-Christophe Michel (Guest)
on 2006-01-11 19:39
(Received via mailing list)
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 :(

Please tell me the right way to reuse logger.
E48d29dc8fedb2878fa518d41cc63d88?d=identicon&s=25 Jan Prill (Guest)
on 2006-01-11 21: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
    @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
This topic is locked and can not be replied to.