Ruby-on-rails 在 rails 3 中设置记录器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6019583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:09:39  来源:igfitidea点击:

Setting up the logger in rails 3

ruby-on-railsrubyruby-on-rails-3logging

提问by John Baker

I'm trying to figure out how to use the logger with rails 3. I need to log to a file not have it in the console, but I just can't figure out how to set it up and then, how to write something to that log. I tried the rails docs but they didn't really make it clear.

我正在尝试弄清楚如何将记录器与 rails 3 一起使用。我需要登录到控制台中没有的文件,但我就是不知道如何设置它,然后,如何写一些东西到那个日志。我尝试了 rails 文档,但他们并没有真正说清楚。

回答by Dylan Markow

By default, Rails should be logging to an environment-specific log file in your project's logdirectory. It will be called either test.log, development.log, or production.logdepending on which environment you're running in.

默认情况下,Rails 应该记录到项目log目录中特定于环境的日志文件。将要么叫test.logdevelopment.logproduction.log取决于你正在运行的环境。

You can log directly to Rails' logger using the Rails.loggerobject:

您可以使用Rails.logger对象直接登录到 Rails 的记录器:

Rails.logger.info "My info message"
Rails.logger.debug "My debugging message"
Rails.logger.warn "My warning message"

Rails used to use Ruby's standard logging class, but it now uses ActiveSupport::BufferedLogger. (The official Ruby on Rails Guidesare incorrect when they say "Rails makes use of Ruby's standard logger to write log information").

Rails 过去使用Ruby 的标准日志记录类,但现在使用ActiveSupport::BufferedLogger. (Ruby on Rails官方指南说“Rails 使用 Ruby 的标准记录器来写入日志信息”是不正确的)。