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

4 回答 4

10

尝试更改日志级别,默认为 info。

来自指南: http: //guides.rubyonrails.org/debugging_rails_applications.html#log-levels

于 2013-06-20T00:39:36.873 回答
7

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

config.log_level = :error

可用的日志级别有::debug:info:warn:error:fatal:unknown,它们对应于整数0- 5

于 2014-08-04T18:01:40.647 回答
1

您可以关闭 SQL 查询。我知道他们占用了很多空间。

在控制台中禁用 Rails SQL 日志记录

于 2013-06-20T00:34:44.697 回答
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

用法

RAILS_LOG_TO_STDOUT=true.env中设置以打开控制台记录器

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

如何在设置环境变量的情况下启动 Rails 服务器?

更多信息

配置 Rails 应用程序

于 2020-02-10T00:34:53.350 回答