Skip to content

Instantly share code, notes, and snippets.

@lenart
Created October 9, 2013 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lenart/6898701 to your computer and use it in GitHub Desktop.
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.
# 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
# app/controllers/pages_controller.rb
class PagesController < ApplicationController
def newrelic_ping
render nothing: true
end
end
# config/routes.rb
MyApp::Application.routes.draw do
# ...
get 'newrelic-ping' => 'pages#newrelic_ping'
# ...
end
# 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