【问题标题】:Rails - how to disable all console logging?Rails - 如何禁用所有控制台日志记录?
【发布时间】:2013-06-16 17:06:46
【问题描述】:

来自 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

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

感谢任何帮助!

【问题讨论】:

    标签: ruby-on-rails ruby console


    【解决方案1】:

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

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

    【讨论】:

    • 我将它设置为0,但我仍然收到警告。
    • 您需要将其设置为 4 或 :fatal。这将过滤掉所有较低级别的消息
    【解决方案2】:

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

    config.log_level = :error
    

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

    【讨论】:

      【解决方案3】:

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

      Disable Rails SQL logging in console

      【讨论】:

      • 虽然这很有用,但我希望禁用 所有 Rails 的通知/日志/whateveryouwanttocall 它们。所以 HTTP 请求、警告,除了错误之外的一切
      【解决方案4】:

      解决方案

      ./.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设置为关闭控制台记录器

      How to start Rails server with environment variables set?

      更多信息

      Configuring Rails Applications

      【讨论】:

        猜你喜欢
        • 2012-11-17
        • 2011-12-07
        • 1970-01-01
        • 1970-01-01
        • 2014-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多