Class: Timber::Contexts::HTTP

Inherits:
Timber::Context
  • Object
show all
Defined in:
lib/timber/contexts/http.rb

Overview

Note:

This context should be installed automatically through the, Intregrations::Rack::HTTPContext Rack middleware.

The HTTP context adds data about the current HTTP request being processed to your logs. This allows you to tail and filter by this data. A very useful piece of data this captures is the request ID. This gives you the ability to trace requests and view logs for a specific request only. For example, say you’ve searched your logs and found the specific line you are looking for, but it lacks context. With Timber you can simply click the request ID and “zoom out” to view all logs for that request. This gives you complete picture of how the log line in questio was generated.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ HTTP

Returns a new instance of HTTP.



20
21
22
23
24
25
# File 'lib/timber/contexts/http.rb', line 20

def initialize(attributes)
  @method = attributes[:method] || raise(ArgumentError.new(":method is required"))
  @path = attributes[:path] || raise(ArgumentError.new(":path is required"))
  @remote_addr = attributes[:remote_addr]
  @request_id = attributes[:request_id]
end

Instance Attribute Details

#methodObject (readonly)

Returns the value of attribute method.



18
19
20
# File 'lib/timber/contexts/http.rb', line 18

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/timber/contexts/http.rb', line 18

def path
  @path
end

#remote_addrObject (readonly)

Returns the value of attribute remote_addr.



18
19
20
# File 'lib/timber/contexts/http.rb', line 18

def remote_addr
  @remote_addr
end

#request_idObject (readonly)

Returns the value of attribute request_id.



18
19
20
# File 'lib/timber/contexts/http.rb', line 18

def request_id
  @request_id
end

Instance Method Details

#as_json(_options = {}) ⇒ Object

Builds a hash representation containing simple objects, suitable for serialization (JSON).



28
29
30
# File 'lib/timber/contexts/http.rb', line 28

def as_json(_options = {})
  {:method => method, :path => path, :remote_addr => remote_addr, :request_id => request_id}
end