chuacw
chuacw

Reputation: 1863

How do I enable logging in Rails?

I'm a Ruby/Rails newbie, but not a development newbie.

I have the following code defined in iniatializer.rb:

def logger
  if defined?(RAILS_DEFAULT_LOGGER)
    RAILS_DEFAULT_LOGGER
  else
    nil
  end
end

I want to enable logging of messages in my Ruby/Rails application.

And in some Ruby/Rails code, I have

  logger.info "Email attachment: #{size} #{filename}"

\ruby\bin>ruby --version gives: ruby 1.8.7 (2010-01-10 patchlevel 249) [i386-mingw32]

How would I change the logger definition so that logger.info goes into a file that I can open up and read? Please include necessary require/include as well.

Thank you.

Upvotes: 4

Views: 2812

Answers (2)

G SubbaRao
G SubbaRao

Reputation: 446

if your rails < 3.X then use below code

def logger
  if defined?(RAILS_DEFAULT_LOGGER)
    Logger.new('log/my_log.log')
  else 
    nil 
  end
end

If your rails > 3.X then replace RAILS_DEFAULT_LOGGER with Rails.logger in above code

def logger
  if defined?(Rails.logger)
    Logger.new('log/my_log.log')
  else 
    nil 
  end
end

Upvotes: 4

Frederick Cheung
Frederick Cheung

Reputation: 84114

RAILS_DEFAULT_LOGGER was removed a long time ago. The new name is Rails.logger. This will be created for you when rails loads.

Upvotes: 1

Related Questions