Project

rails-hush

0.0
A long-lived project that still receives updates
Hushes worthless Rails exceptions & logs, such as those caused by bots and crawlers.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

Runtime

>= 7, < 8.3
 Project Readme

RailsHush

RailsHush hushes worthless Rails exceptions & logs, such as those caused by bots and crawlers.

Rails generates a number of exceptions that, while useful in development, are quite noisy in production. This is expecially true because of the volume of bots deliberately sending malformed traffic to your Rails app. Unfortunately, by default Rails dutifully logs exceptions and backtraces for all of this, and depending on your exception notification system of choice, may trigger unnecessary notifications.

In addition to bots, if your app has an API and is used by third-parties, they may generate a variety of errors while integrating with your app. These too don't need to be logged, but it is useful if the error messages are more helpful than simply "bad request". RailsHush takes care of this for you.

RailsHush will...

  • Silence backtrace logging for a wide array of errors that aren't related to your actual app code
  • Return various 4xx HTTP status codes, instead of 500
  • Provide basic, helpful error messages to 3rd-parties integrating with your app
  • Properly trigger "Completed..." log entries at the end of invalid requests
    • Using standard active_support notifications, for compatibility with non-default logging solutions

RailsHush honors Rails settings for show_exceptions and consider_all_requests_local. With Rails' defaults, it will only activate in production and will not interfere with debugging while in development or test. The exceptions that are hushed are very selective; actual app errors should continue to raise exceptions as normal, even in production.

Installation

Add to Gemfile:

gem 'rails-hush'

Usage

By default, works automatically upon installation. Most of the time, configuration is not necessary.

Configuration

RailsHush does support a couple of configuration options:

To disable automatic loading of middleware:

# Phase one (early) middleware
config.rails_hush.use_one  = false
# Phase two (late) middleware
config.rails_hush.use_two  = false

Then add the middleware on your own:

# Phase one
app.middleware.insert 0, RailsHush::HushOne
# Phase two
app.middleware.insert_after ActionDispatch::DebugExceptions, RailsHush::HushTwo

RailsHush also has a replaceable renderer, in case you don't like the default json/xml output:

config.rails_hush.renderer = lambda do |status, content_type, error|
  # status       - integer, eg: 400
  # content_type - a Mime::Type instance, eg: Mime[:json]
  # error        = RailsHush's default text error message, eg: "some error"
  #                You may replace it with your own based on status or anything else.
  ...
  # the block must return a Rack-compatible response:
  headers = {}
  body = {my: 'payload'}.to_json
  [status, headers, [body]]
end

Testing

Rails' default "test" environment settings are quite different than "production". While the default settings are quite appropriate, with them it is near impossible to test the actual behavior of RailsHush, because it will effectively be disabled in "test". On the whole, it's recommended to just rely on RailsHush's own test suite. However, if you really want to test its behavior inside your app, change the following in environments/test.rb:

config.consider_all_requests_local     = false
config.action_dispatch.show_exceptions = true        # rails <= 7.0
config.action_dispatch.show_exceptions = :rescuable  # rails >= 7.1

Contributing

Contributions are welcomed. If unsure about the scope or reach of a potential PR, feel free to open a discussion Issue first (not required though).

Please submit human-written contributions only. No contributions containing output generated by LLMs or other non-deterministic systems are allowed.

  • Code, tests, docs, PRs: must be 100% human written.
  • Issues, security reports: must originally be 100% human written. If not originally in English, an LLM may be used to translate to English provided that this translation is disclosed.

We recognize that opinions about the use of LLMs and non-deterministic systems vary widely. Still, we respectfully ask that you honor this policy when interacting with this project.

Q: May I use LLMs apart from contributions?

All usage, training or otherwise, must respect copyrights. Beyond that, we've found that LLMs are prone to errors and misunderstandings--sometimes obvious, sometimes subtle. Please verify everything before trusting an LLM's output.

Running tests

bundle install
rake [test]

License

MIT