1

这是过时的吗? http://guides.rubyonrails.org/debugging_rails_applications.html 我正在一步一步地关注它,

在“2.1 什么是记录器?”部分中

它说明了如何创建自定义记录器。

1)您可以在 environment.rb 或任何环境文件中指定替代记录器:

Rails.logger = Logger.new(STDOUT)
Rails.logger = Log4r::Logger.new("Application Log")

我把这段代码放在我的 environment.rb 文件中,我得到了这个:

未初始化的常量 Log4r (NameError)

..然后它说“或者在初始化程序部分,添加以下任何内容:”

config.logger = Logger.new(STDOUT)
config.logger = Log4r::Logger.new("Application Log")

我的 environment.rb 文件中没有初始化程序部分!我犯了同样的错误!

它说“如果您愿意,您也可以替换另一个记录器,例如 Log4r。” 为什么有人要替换现有的记录器?

Rails.logger = Log4r::Logger.new("Application Log")从 environment.rb 文件中删除了。它似乎正在工作,但我期待在 log/ 目录中创建一个文件,但什么也没有。这是我的 enviroment.rb 文件:

# Load the rails application
require File.expand_path('../application', __FILE__)

Rails.logger = Logger.new('Application Log')

# Initialize the rails application
MyApp::Application.initialize!

帮助?请解释一下?

4

2 回答 2

5

You can set the Rails logger globally with:

Rails.logger = ...

or you can put it into an environment file, e.g. in config/environments/development.rb

MyApp::Application.configure do
  config.logger = ...
end

Next, Log4r is a separate gem that does not come with Rails. If you want to use it, add it to your Gemfile:

gem "log4r", "~> 1.1.10"

Why would somebody want to substitute an existing logger?

Because loggers have a different set of features. You might want to use a custom logger to upload the logs to an external service, process it etc.

于 2013-01-18T15:28:49.743 回答
0

您需要在 application.rb 中要求 log4r。我假设你已经安装了 gem。然后在 application.rb

 require 'log4r'
于 2017-03-14T12:50:06.770 回答