File: sinatra-logging.rb

package info (click to toggle)
ruby-cabin 0.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: ruby: 1,306; makefile: 12
file content (27 lines) | stat: -rw-r--r-- 559 bytes parent folder | download | duplicates (4)
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
require "rubygems"
require "sinatra"
$: << "./lib"
require "cabin"
require "logger"

$logger = Cabin::Channel.new
$logger.subscribe(Logger.new(STDOUT))

def serve_it_up(arg)
  $logger.info("Serving it up")
  sleep 2
  "Hello, #{arg}!"
end

get "/hello/:name" do
  context = $logger.context
  context[:name] = params[:name]
  context[:verb] = "GET"
  timer = $logger.time("serve_it_up latency")
  result = serve_it_up(params[:name])
  timer.stop

  # Clear the context so that the next request doesn't have tainted context.
  context.clear
  return result
end