rack-requestash 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -11
- data/lib/rack/requestash.rb +3 -1
- data/lib/rack/requestash/commonjsonlogger.rb +41 -0
- metadata +5 -5
- data/lib/rack/requestash/accesslogger.rb +0 -51
data/README.md
CHANGED
@@ -19,21 +19,17 @@ Or install it yourself as:
|
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
|
-
|
23
|
-
provides Apache-style access logs. In order to enable in in your application,
|
24
|
-
try something like:
|
22
|
+
For rails, add the rack extension in the environment file to enable access logging. Do something like this in the environment.rb:
|
25
23
|
|
24
|
+
require 'rack/requestash'
|
25
|
+
config.middleware.use Rack::Requestash::CommonJsonLogger, Rails.logger
|
26
26
|
|
27
|
-
|
27
|
+
You can pass a different logger. For more information, check `Rack::CommonLogger` class.
|
28
28
|
|
29
|
-
|
30
|
-
use Rack::Requestash::AccessLogger
|
31
|
-
|
32
|
-
get '/' do
|
33
|
-
'Yahtzee!'
|
34
|
-
end
|
35
|
-
end
|
29
|
+
For rack applications, add the below snippet in the config.ru file:
|
36
30
|
|
31
|
+
require 'rack/requestash'
|
32
|
+
use Rack::Requestash::CommonJsonLogger, logger
|
37
33
|
|
38
34
|
You will then get nice, JSON formatted logs:
|
39
35
|
|
data/lib/rack/requestash.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'rack/commonlogger'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
module Requestash
|
6
|
+
class CommonJsonLogger < Rack::CommonLogger
|
7
|
+
def initialize(app, logger=nil)
|
8
|
+
super(app, logger)
|
9
|
+
end
|
10
|
+
|
11
|
+
def log(env, status, header, began_at)
|
12
|
+
#Access the base class variables with caution; they may not exist
|
13
|
+
logger = @logger || env['rack.errors']
|
14
|
+
|
15
|
+
blob = {
|
16
|
+
:length => header['Content-Length'] || 0,
|
17
|
+
:code => status.to_s[0 .. 3],
|
18
|
+
:version => env['HTTP_VERSION'],
|
19
|
+
:method => env['REQUEST_METHOD'],
|
20
|
+
:duration => (Time.now - began_at),
|
21
|
+
:query => env["QUERY_STRING"],
|
22
|
+
:path => env['PATH_INFO'],
|
23
|
+
:remote_addr => env['REMOTE_ADDR'],
|
24
|
+
:user => env['REMOTE_USER'],
|
25
|
+
:user_agent => env['HTTP_USER_AGENT'],
|
26
|
+
:timestamp => Time.now.utc.iso8601
|
27
|
+
}
|
28
|
+
|
29
|
+
# If there's an X-Forwarded-For header split it up into a
|
30
|
+
# list of machine-readable IPs.
|
31
|
+
blob[:forwarded_for] = env['HTTP_X_FORWARDED_FOR'].split(',') if env['HTTP_X_FORWARDED_FOR']
|
32
|
+
|
33
|
+
if logger
|
34
|
+
logger.write({:type => 'request',
|
35
|
+
:event => blob}.to_json)
|
36
|
+
logger.write("\n")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-requestash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-09-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -73,7 +73,7 @@ files:
|
|
73
73
|
- README.md
|
74
74
|
- Rakefile
|
75
75
|
- lib/rack/requestash.rb
|
76
|
-
- lib/rack/requestash/
|
76
|
+
- lib/rack/requestash/commonjsonlogger.rb
|
77
77
|
- lib/rack/requestash/log4r.rb
|
78
78
|
- rack-requestash.gemspec
|
79
79
|
- spec/log4r_spec.rb
|
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
segments:
|
95
95
|
- 0
|
96
|
-
hash:
|
96
|
+
hash: -407596688446818320
|
97
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
98
|
none: false
|
99
99
|
requirements:
|
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: '0'
|
103
103
|
segments:
|
104
104
|
- 0
|
105
|
-
hash:
|
105
|
+
hash: -407596688446818320
|
106
106
|
requirements: []
|
107
107
|
rubyforge_project:
|
108
108
|
rubygems_version: 1.8.25
|
@@ -1,51 +0,0 @@
|
|
1
|
-
require 'rack/commonlogger'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
module Rack
|
5
|
-
module Requestash
|
6
|
-
class AccessLogger
|
7
|
-
def initialize(app, options={})
|
8
|
-
@app = app
|
9
|
-
|
10
|
-
install if options[:disable_accesslog].nil?
|
11
|
-
end
|
12
|
-
|
13
|
-
def call(env)
|
14
|
-
# Simple pass-through, we don't need to mess with the request
|
15
|
-
@app.call(env)
|
16
|
-
end
|
17
|
-
|
18
|
-
# Install the Rack::CommonLogger monkeypatch
|
19
|
-
def install
|
20
|
-
Rack::CommonLogger.class_eval do
|
21
|
-
|
22
|
-
alias_method :original_log, :log
|
23
|
-
|
24
|
-
def log(env, status, header, began_at)
|
25
|
-
logger = @logger || env['rack.errors']
|
26
|
-
|
27
|
-
blob = {
|
28
|
-
:length => header['Content-Length'] || 0,
|
29
|
-
:code => status.to_s[0 .. 3],
|
30
|
-
:version => env['HTTP_VERSION'],
|
31
|
-
:method => env['REQUEST_METHOD'],
|
32
|
-
:duration => (Time.now - began_at),
|
33
|
-
:query => env["QUERY_STRING"],
|
34
|
-
:path => env['PATH_INFO'],
|
35
|
-
:ip => env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR'],
|
36
|
-
:user => env['REMOTE_USER'],
|
37
|
-
:timestamp => Time.now.utc.iso8601
|
38
|
-
}
|
39
|
-
|
40
|
-
if logger
|
41
|
-
logger.write({:type => 'request',
|
42
|
-
:event => blob}.to_json)
|
43
|
-
logger.write("\n")
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|