如何在Rails中启用日志记录?

时间:2022-04-14 22:56:20

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

我是Ruby/Rails新手,但不是开发新手。

I have the following code defined in iniatializer.rb:

我有以下在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.

我希望在我的Ruby/Rails应用程序中启用日志记录。

And in some Ruby/Rails code, I have

在一些Ruby/Rails代码中,我有。

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

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

\ruby\bin>ruby——版本给出: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.

我将如何改变logger定义,让logger.info进入一个我可以打开和读取的文件?请包括必要的要求。

Thank you.

谢谢你!

2 个解决方案

#1


3  

if your rails < 3.X then use below code

如果您的rails < 3。然后使用下面的代码。

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

如果你的rails >3。然后用Rails替换RAILS_DEFAULT_LOGGER。记录器在上面的代码

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

#2


1  

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

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

#1


3  

if your rails < 3.X then use below code

如果您的rails < 3。然后使用下面的代码。

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

如果你的rails >3。然后用Rails替换RAILS_DEFAULT_LOGGER。记录器在上面的代码

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

#2


1  

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

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