Class: Falcon::Middleware::Verbose

Inherits:
Protocol::HTTP::Middleware
  • Object
show all
Defined in:
lib/falcon/middleware/verbose.rb

Overview

A HTTP middleware for logging requests and responses.

Instance Method Summary collapse

Constructor Details

#initialize(app, logger = Console.logger) ⇒ Verbose

Initialize the verbose middleware.



16
17
18
19
20
# File 'lib/falcon/middleware/verbose.rb', line 16

def initialize(app, logger = Console.logger)
	super(app)
	
	@logger = logger
end

Instance Method Details

#annotate(request) ⇒ Object

Log details of the incoming request.



23
24
25
26
27
28
29
30
# File 'lib/falcon/middleware/verbose.rb', line 23

def annotate(request)
	task = Async::Task.current
	address = request.remote_address
	
	@logger.info(request) {"Headers: #{request.headers.to_h} from #{address.inspect}"}
	
	task.annotate("#{request.method} #{request.path} from #{address.inspect}")
end

#call(request) ⇒ Object

Log details of the incoming request using #annotate and wrap the response to log response details too.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/falcon/middleware/verbose.rb', line 33

def call(request)
	annotate(request)
	
	statistics = Async::HTTP::Statistics.start
	
	response = super
	
	statistics.wrap(response) do |statistics, error|
		@logger.info(request) {"Responding with: #{response.status} #{response.headers.to_h}; #{statistics.inspect}"}
		
		@logger.error(request) {"#{error.class}: #{error.message}"} if error
	end
	
	return response
end