Class: Timber::Integrations::Rack::HTTPEvents
- Inherits:
-
Middleware
- Object
- Middleware
- Timber::Integrations::Rack::HTTPEvents
- Defined in:
- lib/timber/integrations/rack/http_events.rb
Overview
A Rack middleware that is reponsible for capturing and logging HTTP server requests and response events. The Events::HTTPRequest and Events::HTTPResponse events respectively.
Class Method Summary collapse
-
.capture_request_body=(value) ⇒ Object
Allows you to capture the HTTP request body, default is off (false).
-
.capture_request_body? ⇒ Boolean
Accessor method for #capture_request_body=.
-
.capture_response_body=(value) ⇒ Object
Just like #capture_request_body= but for the Events::HTTPResponse event.
-
.capture_response_body? ⇒ Boolean
Accessor method for #capture_response_body=.
-
.collapse_into_single_event=(value) ⇒ Object
Collapse both the HTTP request and response events into a single log line event.
-
.collapse_into_single_event? ⇒ Boolean
Accessor method for #collapse_into_single_event=.
-
.silence_request ⇒ Object
Accessor method for #silence_request=.
-
.silence_request=(proc) ⇒ Object
This setting allows you to silence requests based on any conditions you desire.
Instance Method Summary collapse
Constructor Details
This class inherits a constructor from Timber::Integrations::Rack::Middleware
Class Method Details
.capture_request_body=(value) ⇒ Object
Allows you to capture the HTTP request body, default is off (false).
Capturing HTTP bodies can be extremely helpful when debugging issues, but please proceed with caution:
-
Capturing HTTP bodies can use quite a bit of data (this can be mitigated, see below)
-
The Events::ControllerCall event captures the parsed parmaters sent to the controller. This is a parsed representation of the body, which is usually more helpful and redundant to the body captured here.
If you opt to capture bodies, you can also truncate the size to reduce the data captured. See Events::HTTPRequest.
33 34 35 |
# File 'lib/timber/integrations/rack/http_events.rb', line 33 def capture_request_body=(value) @capture_request_body = value end |
.capture_request_body? ⇒ Boolean
Accessor method for #capture_request_body=
38 39 40 |
# File 'lib/timber/integrations/rack/http_events.rb', line 38 def capture_request_body? @capture_request_body == true end |
.capture_response_body=(value) ⇒ Object
Just like #capture_request_body= but for the Events::HTTPResponse event. Please see #capture_request_body= for more details. The documentation there also applies here.
45 46 47 |
# File 'lib/timber/integrations/rack/http_events.rb', line 45 def capture_response_body=(value) @capture_response_body = value end |
.capture_response_body? ⇒ Boolean
Accessor method for #capture_response_body=
50 51 52 |
# File 'lib/timber/integrations/rack/http_events.rb', line 50 def capture_response_body? @capture_response_body == true end |
.collapse_into_single_event=(value) ⇒ Object
Collapse both the HTTP request and response events into a single log line event. While we don’t recommend this, it can help to reduce log volume if desired. The reason we don’t recommend this, is because the logging service you use should not be so expensive that you need to strip out useful logs. It should also provide the tools necessary to properly search your logs and reduce noise. Such as viewing logs for a specific request.
To provide an example. This setting turns this:
Started GET "/" for 127.0.0.1 at 2012-03-10 14:28:14 +0100
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)
Into this:
Get "/" sent 200 OK in 79ms
The single event is still a Events::HTTPResponse event. Because we capture HTTP context, you still get the HTTP details, but you will not get all of the request details that the Events::HTTPRequest event would provide.
77 78 79 |
# File 'lib/timber/integrations/rack/http_events.rb', line 77 def collapse_into_single_event=(value) @collapse_into_single_event = value end |
.collapse_into_single_event? ⇒ Boolean
Accessor method for #collapse_into_single_event=.
82 83 84 |
# File 'lib/timber/integrations/rack/http_events.rb', line 82 def collapse_into_single_event? @collapse_into_single_event == true end |
.silence_request ⇒ Object
Accessor method for #silence_request=
104 105 106 |
# File 'lib/timber/integrations/rack/http_events.rb', line 104 def silence_request @silence_request end |
.silence_request=(proc) ⇒ Object
This setting allows you to silence requests based on any conditions you desire. We require a block because it gives you complete control over how you want to silence requests. The first parameter being the traditional Rack env hash, the second being a [Rack Request](www.rubydoc.info/gems/rack/Rack/Request) object.
95 96 97 98 99 100 101 |
# File 'lib/timber/integrations/rack/http_events.rb', line 95 def silence_request=(proc) if proc && !proc.is_a?(Proc) raise ArgumentError.new("The value passed to #silence_request must be a Proc") end @silence_request = proc end |
Instance Method Details
#call(env) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/timber/integrations/rack/http_events.rb', line 109 def call(env) request = Util::Request.new(env) if silenced?(env, request) if Config.instance.logger.respond_to?(:silence) Config.instance.logger.silence do @app.call(env) end else @app.call(env) end elsif collapse_into_single_event? start = Time.now status, headers, body = @app.call(env) Config.instance.logger.info do http_context_key = Contexts::HTTP.keyspace http_context = CurrentContext.fetch(http_context_key) time_ms = (Time.now - start) * 1000.0 Events::HTTPResponse.new( headers: headers, http_context: http_context, request_id: request.request_id, status: status, time_ms: time_ms ) end [status, headers, body] else start = Time.now Config.instance.logger.info do event_body = capture_request_body? ? request.body_content : nil Events::HTTPRequest.new( body: event_body, headers: request.headers, host: request.host, method: request.request_method, path: request.path, port: request.port, query_string: request.query_string, request_id: request.request_id, # we insert this middleware after ActionDispatch::RequestId scheme: request.scheme ) end status, headers, body = @app.call(env) Config.instance.logger.info do time_ms = (Time.now - start) * 1000.0 event_body = capture_response_body? ? body : nil Events::HTTPResponse.new( body: event_body, headers: headers, request_id: request.request_id, status: status, time_ms: time_ms ) end [status, headers, body] end end |