Class: Utopia::Middleware::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/middleware/logger.rb

Constant Summary collapse

ACCESS_LOG =
"access_log"
HEADER =
[:ip, :agent, :method, :url, :status, :location, :length]

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Logger

Returns a new instance of Logger.



51
52
53
54
55
# File 'lib/utopia/middleware/logger.rb', line 51

def initialize(app, options = {})
	@app = app
	
	@log = options[:log] || TimeStore.new(options[:path] || ACCESS_LOG, options[:header] || HEADER)
end

Instance Method Details

#call(env) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/utopia/middleware/logger.rb', line 57

def call(env)
	response = @app.call(env)

	Thread.new do
		write_log(env, response)
	end

	return response
end

#write_log(env, response) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/utopia/middleware/logger.rb', line 26

def write_log(env, response)
	request = Rack::Request.new(env)

	record = {
		:ip => request.ip,
		:host => request.host,
		:url => request.url,
		:agent => env['HTTP_USER_AGENT'],
		:status => response[0],
		:method => request.request_method,
		:user => env['REMOTE_USER'],
		:version => env['HTTP_VERSION']
	}

	if response[1].key? "Location"
		record[:location] = response[1]["Location"]
	end
	
	if response[1].key? "Content-Length"
		record[:length] = response[1]["Content-Length"]
	end

	@log << record
end