No commit activity in last 3 years
No release in over 3 years
Log HTTP requests via Rack stack to an object. You can use any object, i.e. ActiveRecord model
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 6.0, < 7.0
~> 2.0
~> 0.5
~> 0
~> 12.3
~> 3.8
~> 1.4

Runtime

> 0, < 3.0
 Project Readme

Build Status Gem Version License

rack-request-object-logger

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.

Install gem

gem install rack-request-object-logger

Gemfile

gem 'rack-request-object-logger'

Rubies Support

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.

Roadmap

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)

Performance

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.

Rails Integration/Awareness

The logger sets the UUID of request to match the request ID set by Rails.

Security considerations

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 LICENSE)

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.

...Enjoy

Development

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.

Contributing

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.

Alternatives

There may be better alternatives for you: