Rack::SimpleLogger
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
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request