Sha256: 4e0eac150741ee0a10dbca614a3655d74e26eecf78d30639b221ef5f47888936

Contents?: true

Size: 966 Bytes

Versions: 7

Compression:

Stored size: 966 Bytes

Contents

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('/dev/null')}
  end
end

App.run

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
webmachine-1.6.0 examples/logging.rb
webmachine-1.5.0 examples/logging.rb
webmachine-1.4.0 examples/logging.rb
webmachine-1.3.1 examples/logging.rb
webmachine-1.3.0 examples/logging.rb
webmachine-1.2.2 examples/logging.rb
webmachine-1.2.0 examples/logging.rb