Created
October 9, 2013 09:34
-
-
Save lenart/6898701 to your computer and use it in GitHub Desktop.
SilentLogger for Rails 4 to keep NewRelic's requests out of the production log.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/application.rb | |
require File.expand_path('../boot', __FILE__) | |
require 'rails/all' | |
require File.dirname(__FILE__) + '/../lib/silent_logger.rb' | |
# Require the gems listed in Gemfile, including any gems | |
# you've limited to :test, :development, or :production. | |
Bundler.require(:default, Rails.env) | |
module Bps | |
class Application < Rails::Application | |
# ... | |
# Don't log NewRelic ping requests | |
config.middleware.swap Rails::Rack::Logger, SilentLogger, silence: ['/newrelic-ping'] | |
# ... | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/pages_controller.rb | |
class PagesController < ApplicationController | |
def newrelic_ping | |
render nothing: true | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/routes.rb | |
MyApp::Application.routes.draw do | |
# ... | |
get 'newrelic-ping' => 'pages#newrelic_ping' | |
# ... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# lib/silent_logger.rb | |
# | |
# SilentLogger can be used to keep requests out of log file | |
# It is best used for ping requests from uptime services like | |
# NewRelic, Uptime Robot, etc. | |
# | |
# Source: http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/ | |
# | |
# Include logger in your config/application.rb after require 'rails/all' | |
# require File.dirname(__FILE__) + '/../lib/silent_logger.rb' | |
# | |
# In application.rb or environments/production.rb swap Rails logger: | |
# config.middleware.swap Rails::Rack::Logger, SilentLogger, silenced: ['/newrelic-ping'] | |
# | |
class SilentLogger < Rails::Rack::Logger | |
def initialize(app, taggers = nil) | |
@app = app | |
@taggers = taggers | |
if @taggers.keys.include? :silence | |
@silenced_paths = @taggers[:silence] | |
@taggers.delete(:silence) | |
end | |
super | |
end | |
def call(env) | |
if env['X-SILENCE-LOGGER'] || @silenced_paths.include?(env['PATH_INFO']) | |
Rails.logger.silence do | |
@app.call(env) | |
end | |
else | |
super(env) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment