English | 日本語
To capture the request and response in the rack middleware, you can be output to any destination
Has the following features:
- It does not automatically run the capture of the request and response
- Very customizable
- middleware has the defult output destination, but can be set independently of the output destination.
- You can customize the content you want to capture.
Immediately after installation , you do not automatically have not able to run the capture.
Including the setting to the top of the rack middlewares, it will work .
install it via rubygems:
gem install rack-access-captureor put it in your Gemfile:
# Gemfile gem 'rack-access-capture'Then insert Rack::Access::Capture::Manager and the setting of middlware on the head of rack middlewares.
Additional settings by source code:
config.middleware.use Rack::Access::Capture::Manager do |config| config.collector = { adapter: :fluentd, config: { host: 'localhost', port: 24224, tag: 'mytag', exclude_user_agents: ["exclude_user_agent_1", "exclude_user_agent_2"] } } config.watcher = { adapter: 'MyWatcher' } config.filter = { params: ['params1', 'params2'] } endor Additional settings by YAML:
config.middleware.use Rack::Access::Capture::Manager, YAML.load_file("#{Rails.root}/config/rack.yml")collector: adapter: fluentd config: host: localhost port: 24224 tag: my_tag exclude_user_agents: - "exclude_user_agent_1" - "exclude_user_agent_2" watcher: adapter: MyWatcher filter: params: - password - email - name - bodyDefault output format:
{"status":200,"path":"/","method":"GET","params":"{}","device":"pc","os":"Mac OSX","browser":"Chrome","browser_ver":"50.0.2661.102","user_agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36","remote_ip":"::1","time":1466588840,"accessed_at":1466588840,"app_exec_time":3.8547658920288086} status: Response Status Codepath: Request Pathmethod: Request Methodparams: Request Parameterdevice: Originating device(via User Agent)os: Operating systems(via User Agent)browser: Browser name(via User Agent)browser_ver: Browser version(via User Agent)user_agent: User Agentremote_ip: Originating IP addresstime: Capture execution timeaccessed_at: Access timeapp_exec_time: The rack application execution time in seconds. milli seconds are written after the decimal point.
Currently the options you can change are as follows:
Set of output destination to the collector.
- adapter: This specifies the implementation class of the output destination.(default:
console)- The captured content is output to standard output.(Set the
console.) - The captured content is output to fluentd.(Set the
fluentd.) - Implementation class name of the customized output destination.
- The captured content is output to standard output.(Set the
- config: Specifies the adapter settings.
console: No settings.fluentd: Set the setting value of fluent-logger-ruby.
format: If you specify a ltsv output in LTSV format. If you specify a json output in Json format. The default is json output.
Use LTSV format.
collector: adapter: console config: format: ltsvSet of customized capture function to the watcher.
- adapter: This specifies the implementation class name.(The default is undefined.)
Set of a mask of request parameters to the watcher.
The default is to the following two.
- password
- authenticity_token
the output destination and the capture you can change are as follows:
By specifying the name of the class of its own adapter implementation to the collector,
you will be able to use in the implementation of their own output destination.
require 'rack-access-capture' class YourCustomizedCollectorAdapter < Rack::Access::Capture::Collector::AbstractAdapter def initialize(*options) @config = options["adapter_config"] end def collect?(env) env["REQUEST_METHOD"] != "GET" end def collect(log) puts log end endBy specifying the name of the class of its own adapter implementation to the watcher,
you will be able to use in the implementation of their own capture function.
require 'rack-access-capture' module Rack class YourCustomizedWatcher < Rack::Access::Capture::Watcher::BaseAdapter def request_capture(env) { forwardedfor: env['HTTP_X_FORWARDED_FOR'] } end def response_capture(env, http_status_code, header) { rails_action: env[:rails_action] } end end endIf you want to use your own settings, lists the file as follows:
collector: adapter: YourCustomizedCollectorAdapter config: adapter_config: custom_collector_config watcher: adapter: YourCustomizedWatcher filter: params: - password - email - name - body- 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
See LICENSE.txt for details.