forked from webmachine/webmachine-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.rb
More file actions
41 lines (33 loc) · 959 Bytes
/
logging.rb
File metadata and controls
41 lines (33 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
require 'webmachine'
require 'time'
require 'logger'
class HelloResource < Webmachine::Resource
def to_html
"<html><head><title>Hello from Webmachine</title></head><body>Hello, world!</body></html>\n"
end
end
class LogListener
def call(*args)
handle_event(Webmachine::Events::InstrumentedEvent.new(*args))
end
def handle_event(event)
request = event.payload[:request]
resource = event.payload[:resource]
code = event.payload[:code]
puts '[%s] method=%s uri=%s code=%d resource=%s time=%.4f' % [
Time.now.iso8601, request.method, request.uri.to_s, code, resource,
event.duration
]
end
end
Webmachine::Events.subscribe('wm.dispatch', LogListener.new)
App = Webmachine::Application.new do |app|
app.routes do
add_route [], HelloResource
end
app.configure do |config|
config.adapter = :WEBrick
config.adapter_options = {AccessLog: [], Logger: Logger.new(File::NULL)}
end
end
App.run