Human description: I created this to log all HTTP requests from my Rails application into MySQL database automatically. Then process and search via ElasticSearch.
General concept: Log HTTP requests via Rack stack to an object. Use any object, because logger uses dependency injection in the constructor. Be independent from Rails.
Don't be confused with no commits in months or years. Rack middlewares rarely change. They just work.
gem install rack-request-object-logger
Tested with Matz Ruby 2.5, 2.6 and Truffleruby.
Should work with Ruby 2.1+, jRuby and Rubinius 3.69+ (is dead!) and Truffleruby.
While the code works flawlessly
Bug: I learned at EuRuKo 2018 that my implementation of timings is wrong and not very accurate.
Example - logging to SQL database in Rails
generate a model for storage
# you need to add 'limit: 6' to application_server_request_start and application_server_request_end # to have subsecond resolution please see examples/db/migrate/ $ bin/rails g model AnalyticsHttpRequest uid:string data:text status_code:integer application_server_request_start:datetime application_server_request_end:datetime
add JSON serialization
# app/models/analytics_http_request.rb class AnalyticsHttpRequest < ApplicationRecord serialize :data, JSON end
add automatic logging via initializer
# config/initializers/rack_middlewares.rb Rails.application.config.middleware.use(RackRequestObjectLogger, AnalyticsHttpRequest)
To run performance tests on your computer run
rspec performance/. On my i5 laptop with ActiveRecord it processes and stores 500 logs per second, with dummy class 5000.
The logger sets the UUID of request to match the request ID set by Rails.
The middleware stores all HTTP headers, but strips all active_dispatch, warden and other stuff. That means HTTP basic auth credentials are stored and also data in query string.
I've seen applications sending sensitive data in GET and even POST requests in a query string. Don't do that. Use POST body. Or modify the middleware to filter out them.
License & Author
Copyright 2016-2019 Ivan Stana
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 (or see the file
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
After checking out the repo, run
bin/setup to install dependencies. Then, run
rake spec to run the tests. You can also run
bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run
bundle exec rake install. To release a new version, update the version number in
version.rb, and then run
bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the
.gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/istana/rack-request-object-logger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
There may be better alternatives for you: