Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add common logging

  • Loading branch information...
commit 43af93d1630e3136ec73e8ebbb832d662b326457 1 parent 25f7251
@ddollar ddollar authored
Showing with 44 additions and 1 deletion.
  1. +4 −0 config.ru
  2. +0 −1  lib/stdout.rb
  3. +40 −0 web/toolbelt_common_logger.rb
View
4 config.ru
@@ -1,5 +1,9 @@
$stdout.sync = true
$:.unshift File.expand_path("../web", __FILE__)
+
+require "toolbelt_common_logger"
require "toolbelt"
+
+use ToolbeltCommonLogger
run Toolbelt
View
1  lib/stdout.rb
@@ -1,2 +1 @@
-#!/usr/bin/env ruby
$stdout.sync = true
View
40 web/toolbelt_common_logger.rb
@@ -0,0 +1,40 @@
+class ToolbeltCommonLogger
+
+ FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n}
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ began_at = Time.now
+ status, headers, body = @app.call(env)
+ ended_at = Time.now
+
+ header_hash = Rack::Utils::HeaderHash.new(headers)
+ length = extract_content_length(header_hash)
+
+ puts FORMAT % [
+ env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
+ env["REMOTE_USER"] || "-",
+ ended_at.strftime("%d/%b/%Y %H:%M:%S"),
+ env["REQUEST_METHOD"],
+ env["PATH_INFO"],
+ env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
+ env["HTTP_VERSION"],
+ status.to_s[0..3],
+ length,
+ ended_at - began_at
+ ]
+
+ [status, headers, body]
+ end
+
+private
+
+ def extract_content_length(headers)
+ value = headers['Content-Length'] or return '-'
+ value.to_s == '0' ? '-' : value
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.