Build Status Dependency Status Code Climate 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

Ruby 2.1+, jruby and rubinius...most likely, because the code is in pure Ruby

TODO

  • the code work flawlessly
  • performance specs don't work on jruby, but works on MRI
  • I couldn't install rubinius yet, so it's untested

Example - logging to SQL database in Rails

generate a model for storage

$ bin/rails g model Sql::HttpRequest uuid:string data:text

add JSON serialization

# app/models/sql/http_request.rb
class Sql::HttpRequest < ApplicationRecord
  serialize :data, JSON
end

add automatic logging via initializer

# config/initializers/rack_middlewares.rb

Rails.application.config.middleware.use(RackRequestObjectLogger, Sql::HttpRequest)

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 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/starmammoth/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: