创建自定义 Rails 记录器 [英] Create a custom Rails Logger
问题描述
这已经过时了吗?http://guides.rubyonrails.org/debugging_rails_applications.html我正在一步一步地跟踪它,
在2.1 什么是 Logger?"部分?
它说明了如何创建自定义记录器.
1) 您可以在您的 environment.rb 或任何环境文件中指定替代记录器:
Rails.logger = Logger.new(STDOUT)Rails.logger = Log4r::Logger.new("应用程序日志")
我把这段代码放在我的 environment.rb 文件中,我得到了这个:
<块引用>未初始化的常量 Log4r (NameError)
..然后它说'或者在初始化部分,添加以下任何一项:'
config.logger = Logger.new(STDOUT)config.logger = Log4r::Logger.new("应用程序日志")
我的 environment.rb 文件中没有初始化程序部分!我得到了同样的错误!
它说'如果你愿意,你也可以替换另一个记录器,比如 Log4r.'为什么有人要替换现有的记录器?
我从 environment.rb 文件中删除了 Rails.logger = Log4r::Logger.new("Application Log")
.它似乎正在工作,但我期待在 log/目录中创建一个文件,但什么也没有.这是我的 environment.rb 文件:
#加载rails应用需要 File.expand_path('../application', __FILE__)Rails.logger = Logger.new('应用程序日志')# 初始化 Rails 应用程序MyApp::Application.initialize!
帮助?请解释一下?
您可以通过以下方式全局设置 Rails 记录器:
Rails.logger = ...
或者您可以将其放入环境文件中,例如在 config/environments/development.rb
MyApp::Application.configure 做config.logger = ...结尾
接下来,Log4r
是一个独立的 gem,它不随 Rails 提供.如果您想使用它,请将其添加到您的 Gemfile 中:
gem "log4r", "~> 1.1.10"
<块引用>
为什么有人想要替换现有的记录器?
因为记录器具有一组不同的功能.您可能希望使用自定义记录器将日志上传到外部服务,进行处理等.
Is this outdated? http://guides.rubyonrails.org/debugging_rails_applications.html I am following it step by step,
And in part '2.1 What is the Logger?'
It says how to create a custom logger.
1) You can specify an alternative logger in your environment.rb or any environment file:
Rails.logger = Logger.new(STDOUT)
Rails.logger = Log4r::Logger.new("Application Log")
I put this code in my environment.rb file and I get this:
uninitialized constant Log4r (NameError)
..then it says 'Or in the Initializer section, add any of the following:'
config.logger = Logger.new(STDOUT)
config.logger = Log4r::Logger.new("Application Log")
I dont have an initializer section in my environment.rb file! I get the same error!
It says 'You can also substitute another logger such as Log4r if you wish.' Why would somebody want to substitute an existing logger?
I removed Rails.logger = Log4r::Logger.new("Application Log")
from the environment.rb file.
It seems to be working, but I was expecting a file being created in the log/ directory but nothing. Here is my enviroment.rb file:
# Load the rails application
require File.expand_path('../application', __FILE__)
Rails.logger = Logger.new('Application Log')
# Initialize the rails application
MyApp::Application.initialize!
help? Some explanation please?
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.
这篇关于创建自定义 Rails 记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!