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, :referer, :length]

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Logger.



45
46
47
48
49
# File 'lib/utopia/middleware/logger.rb', line 45

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



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

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

	Thread.new do
		write_log(env, response)
	end

	return response
end

#write_log(env, response) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/utopia/middleware/logger.rb', line 15

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

	record = {
		:ip => request.ip,
		:host => request.host,
		:url => request.url,
		:referer => request.referer,
		: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
	
	if UTOPIA_ENV != :production
		$stderr.puts ">> #{record[:method]} #{record[:url]} -> #{response[0]}"
	end
end