4

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.

4

2 回答 2

4

如果你的 rails < 3.X 然后使用下面的代码

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

如果您的 rails > 3.X 然后在上面的代码中用 Rails.logger 替换 RAILS_DEFAULT_LOGGER

def logger
  if defined?(Rails.logger)
    Logger.new('log/my_log.log')
  else 
    nil 
  end
end
于 2013-05-07T05:37:22.243 回答
1

RAILS_DEFAULT_LOGGER很久以前就被删除了。新名称是Rails.logger。这将在 Rails 加载时为您创建。

于 2013-05-06T07:17:33.950 回答