ruby-on-rails Rails日志太冗长

7cjasjjr  于 2023-05-23  发布在  Ruby
关注(0)|答案(6)|浏览(120)

如何防止Rails记录太多?下面是我的production.log文件中的典型跟踪,许多部分,缓存命中...它在开发中很有用,但我不想在我的生产环境中使用它。

Started GET "/?redirected=true" for 46.193.131.53 at 2012-08-16 18:39:20 +0200
Processing by HomeController#index as HTML
  Parameters: {"redirected"=>"true"}
  Rendered application/_successfully_connected.html.haml (0.8ms)
  Rendered hotspot_infos/_infos.html.haml (0.4ms)
  Rendered application/_hotspot_infos.html.haml (1.8ms)
  Rendered application/_news.html.haml (0.3ms)
Read fragment views/social-zone-341-directory (0.5ms)
  Rendered application/_directory.html.haml (2.5ms)
  Rendered application/_meteo.html.haml (1.1ms)
  Rendered application/_notifications.html.haml (0.8ms)
  Rendered application/_like_button.html.haml (0.3ms)
  Rendered application/_navbar.html.haml (4.2ms)
  Rendered application/_connection.html.haml (0.5ms)
  Rendered application/_gallery.html.haml (0.2ms)
  Rendered application/_search_bar.html.haml (0.4ms)
  Rendered pictures/_picture_frame.html.haml (0.3ms)
  Rendered application/_profile_preview.html.haml (1.4ms)
  Rendered application/_profile_block.html.haml (1.7ms)
  Rendered application/_menus.html.haml (3.3ms)
  Rendered application/_left_pane.html.haml (5.5ms)
  Rendered application/_langs.html.haml (0.8ms)
  Rendered application/_footer.html.haml (1.9ms)
  Rendered application/_flash_modal.html.haml (0.1ms)
  Rendered application/_connection_required.js.erb (0.2ms)
Completed 200 OK in 159ms (Views: 25.5ms | ActiveRecord: 88.0ms)

谢谢你的帮助
PS:我使用的是Rails 3.2.6

v1l68za4

v1l68za41#

在Rails 4中,将有一个清理日志的设置:

config.action_view.logger = nil

要在Rails 3中实现这一点,您必须对ActionView进行修补:

module ActionView
  class LogSubscriber < ActiveSupport::LogSubscriber
    def logger
      @memoized_logger ||= Logger.new('/dev/null')
    end
  end
end
ipakzgxi

ipakzgxi2#

您需要以不同的方式设置config.log_level。了解日志级别
例如,将以下内容添加到config/evironments/production.rb

config.log_level = :warn

您的可能设置为:debug:info

1l5u6lss

1l5u6lss3#

我把这个放在我的初始化器中,以monkeypatch某些日志,以进行调试而不是信息:

module ActionView
  class LogSubscriber
    def render_template(event)
      message = "Rendered #{from_rails_root(event.payload[:identifier])}"
      message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
      message << " (#{event.duration.round(1)}ms)"
      debug(message)
    end
    alias :render_partial :render_template
    alias :render_collection :render_template
  end  
end

module ActionController
  class LogSubscriber
    # Use debug logging for read_fragment
    # %w(write_fragment read_fragment exist_fragment? expire_fragment expire_page write_page).each do |method|
    %w(read_fragment).each do |method|
      class_eval <<-METHOD, __FILE__, __LINE__ + 1
        def #{method}(event)
          return unless logger.info?
          key_or_path = event.payload[:key] || event.payload[:path]
          human_name  = #{method.to_s.humanize.inspect}
          debug("\#{human_name} \#{key_or_path} (\#{event.duration.round(1)}ms)")
        end
      METHOD
    end
  end
end
shstlldc

shstlldc4#

在2021年,我建议使用semantic_logger(和rails_semantic_logger)gem来实现这一点。(Docs link
Lograge也是一个选项,但目前看来维护得不太好。

4smxwvx5

4smxwvx55#

日志级别可以在每个组件的基础上设置。例如,可以将常规log_level设置为INFO,但将 components 设置为更安静的级别。

# config/environments/production.rb
config.log_level = :info

# Build quieter loggers for particular components.
build_logger = ->(level) {
  # This is the recommend way to build a logger, from
  # https://guides.rubyonrails.org/v6.1/configuring.html
  logger = ActiveSupport::Logger.new(STDOUT, level: level)
  logger.formatter = config.log_formatter
  ActiveSupport::TaggedLogging.new(logger)
}
config.action_controller.logger = build_logger[::Logger::ERROR]
config.active_job.logger = build_logger[::Logger::ERROR]
config.action_view.logger = build_logger[::Logger::ERROR]
config.assets.logger = build_logger[::Logger::ERROR]
config.action_mailer.logger = build_logger[::Logger::ERROR]

这是我的团队正在考虑的生产应用程序的配置,吞吐量为每分钟2-3千个请求。它将允许我们为重要的业务逻辑编写低容量的信息级日志,而不会因为来自ActionController等 * 组件 * 的高容量信息级日志而使我们的日志预算不堪重负。

7fyelxc5

7fyelxc56#

要删除部分日志记录,可以从初始化器中执行此操作:

ActiveSupport.on_load(:action_view) do
  ActiveSupport::Notifications.unsubscribe "render_partial.action_view"
end

相关问题