Rails - 如何禁用所有控制台日志记录?

12

作为一个有 JavaScript 背景的人,在运行 Rails 时我觉得命令行很杂乱。每次发生什么事情,我的命令行都会被一堆垃圾填满。例如:

[2013-06-19 20:25:53] WARN  Could not determine content-length of response body.
 Set content-length of the response or set Response#chunked = true

如何关闭此功能,以便我只看到自己的日志(当然还有错误信息)?

感谢任何帮助!

4个回答

10

我将其设置为0,但仍然收到警告。 - starscape
4
需要将其设置为4或:fatal。这将过滤掉所有较低级别的消息。 - brewp

7

config/environments/development.rb 和/或 testing.rb 中更改日志级别:

config.log_level = :error

日志级别可以有以下选择::debug:info:warn:error:fatal:unknown,它们与整数 0-5 相对应。

1

虽然这很有用,但我想禁用Rails的所有通知/日志/无论你想称之为什么。所以HTTP请求、警告、除了错误之外的一切。 - starscape

0

解决方案

./.env

通常文件.env用于存储应用程序环境中的所有重要/机密属性。如果不存在,请创建.env并添加以下代码:

RAILS_LOG_TO_STDOUT=true

./config/environments/*.rb

编辑您的环境文件(例如./config/environments/development.rb),以创建可断开的日志记录系统。

Rails.application.configure do

  # ...

  if ENV['RAILS_LOG_TO_STDOUT'] == 'true'
    logger           = ActiveSupport::Logger.new(STDOUT)
    # To support a formatter, you must manually assign a formatter from the config.log_formatter value to the logger.
    logger.formatter = config.log_formatter

    # config.logger is the logger that will be used for Rails.logger and any 
    # related Rails logging such as ActiveRecord::Base.logger.
    # It defaults to an instance of ActiveSupport::TaggedLogging that wraps an 
    # instance of ActiveSupport::Logger which outputs a log to the log/ directory.
    config.logger    = ActiveSupport::TaggedLogging.new(logger)

    # config.log_level defines the verbosity of the Rails logger. 
    # This option defaults to :debug for all environments. 
    # The available log levels are: :debug, :info, :warn, :error, :fatal, and :unknown
    #config.log_level = :debug

    # config.log_tags accepts a list of: methods that the request object responds to,
    # a Proc that accepts the request object, or something that responds to to_s. 
    # This makes it easy to tag log lines with debug information like subdomain and request id -
    # both very helpful in debugging multi-user production applications.
    config.log_tags = [:request_id]
  end
end

使用方法

.env中设置RAILS_LOG_TO_STDOUT=true开启控制台日志记录器。

删除或注释掉RAILS_LOG_TO_STDOUT=true行,或在.env中设置RAILS_LOG_TO_STDOUT=false关闭控制台日志记录器。

如何使用环境变量启动Rails服务器?

更多信息

配置Rails应用程序


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接