使用Loggly和lograge禁用Rails 4.2中特定控制器操作的日志记录

时间:2015-10-20 19:34:18

标签: ruby-on-rails ruby-on-rails-4

我们的Rails日志充满了来自我们的负载均衡器的状态ping,过滤它们会很棒。

我们希望记录所有其他端点,但我们希望能够单独关闭此控制器的应用程序日志记录。

控制器如下所示:

class V2::StatusController < ApplicationController
    skip_before_filter :authenticate

    def ping
      head :ok, text: 'OK'
    end

end

编辑:我们的日志记录设置为发送给Loggly:

require 'syslogger'
config.logger = Syslogger.new("APP_NAME", Syslog::LOG_PID, Syslog::LOG_LOCAL7)
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Json.new

1 个答案:

答案 0 :(得分:5)

由于您正在使用lograge,这是来自lograge的自述文件,了解如何忽略操作:

  

要进一步清理日志记录,您还可以告诉Lograge跳过符合给定条件的日志消息。您可以跳过从某些控制器操作生成的日志消息,也可以编写自定义处理程序以根据日志事件中的数据跳过消息:

# config/environments/production.rb
MyApp::Application.configure do
  config.lograge.enabled = true

  config.lograge.ignore_actions = ['home#index', 'aController#anAction']
  config.lograge.ignore_custom = lambda do |event|
    # return true here if you want to ignore based on the event
  end
end

所以你的具体可能就是

config.logger = Syslogger.new("APP_NAME", Syslog::LOG_PID, Syslog::LOG_LOCAL7)
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.ignore_actions = ["v2/status#ping", ...]