Class: Semlogger::Rack

Inherits:
Rails::Rack::Logger
  • Object
show all
Defined in:
lib/semlogger/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, tags = nil, data = nil) ⇒ Rack

Returns a new instance of Rack.



2
3
4
5
# File 'lib/semlogger/rack.rb', line 2

def initialize app, tags = nil, data = nil
	super app, tags
	@data = data || {}
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/semlogger/rack.rb', line 15

def call env
	request = ActionDispatch::Request.new env
	compute_tagged_ request do
		compute_data_ request do
			call_app request, env
		end
	end
end

#call_app(request, env) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/semlogger/rack.rb', line 7

def call_app request, env
	path = request.filtered_path
	Rails.logger.custom( :connection, request.ip, Thread.current.object_id, request.request_method, path).info
	@app.call env
ensure
	ActiveSupport::LogSubscriber.flush_all!
end

#compute_data(request) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/semlogger/rack.rb', line 40

def compute_data request
	data = @data.dup
	data.each do |k, v|
		case v
		when Proc
			data[k] = v.call request
		when Symbol
			data[k] = request.send v
		end
	end
end

#compute_data_(request) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/semlogger/rack.rb', line 32

def compute_data_ request
	if Rails.logger.respond_to? :data
		Rails.logger.data( compute_data( request)) { yield }
	else
		yield
	end
end

#compute_tagged_(request) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/semlogger/rack.rb', line 24

def compute_tagged_ request
	if Rails.logger.respond_to? :tagged
		Rails.logger.tagged( compute_tags( request)) { yield }
	else
		yield
	end
end