No commit activity in last 3 years
No release in over 3 years
Simple logger for rack
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
>= 0
>= 0
 Project Readme

Rack::SimpleLogger

Gem Version Build Status Coverage Status Code Climate

Simple logger for rack.

Installation

Add this line to your application's Gemfile:

gem 'rack-simple_logger'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-simple_logger

Usage

Include as Rack middleware to config.ru.

for local file:

require "rack_application"
require "rack/simple_logger"

use Rack::SimpleLogger, log: File.expand_path("../log/production.log", __FILE__)
run RackApplication.new

for MongoDB:

require "rack_application"
require "mongo"
require "rack/simple_logger"

client = Mongo::Connection.new("localhost", 27017)
database = client["logs"]
collection = database["racklog"]

use Rack::SimpleLogger, log: collection
run RackApplication.new

for CustomLogger:

Instance of a class with the write method.

require "rack_application"
require "rack/simple_logger"

MyLogger = Class.new do
  define_method :write do |log_hash|
    # something log output process
  end
end

use Rack::SimpleLogger, log: MyLogger.new
run RackApplication.new

Change the contents of the log to be output

LogProxy#write to output all the hash of the logs received.
You can change the contents of the log to be output by specifying the :filter.
Custom filter class with a pass method.

pass method is shaped to receive the following parameters:

  • env => Request of rack applications.
  • status => HTTP status code.
  • header => HTTP response header.
  • began_at => Time of the request received.
require "rack_application"
require "rack/simple_logger"

MyFilter = Class.new do
  define_method :pass do |env,status,header,began_at|
    {
      xff: env["HTTP_X_FORWARDED_FOR"] || "-",
      host: env["REMOTE_ADDR"],
      time: began_at.strftime("%Y-%m-%d %H:%M:%S"),
      method: env["REQUEST_METHOD"],
      path: env["PATH_INFO"],
      query_strings: env["QUERY_STRING"] || "-",
      status: status,
      ua: env["HTTP_USER_AGENT"],
      res_size: header["Content-Length"],
      app_time: Time.now - began_at
    }
  end
end

use Rack::SimpleLogger, log: "/path/to/production.log", filter: MyFilter.new
run RackApplication.new

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request