rack-requestash 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -11,4 +11,5 @@ group :test do
11
11
  gem 'debugger', :platform => :mri_19
12
12
  gem 'debugger-pry', :platform => :mri_19
13
13
  gem 'pry'
14
+ gem 'log4r'
14
15
  end
data/README.md CHANGED
@@ -27,7 +27,7 @@ try something like:
27
27
  require 'rack/requestash'
28
28
 
29
29
  class MyServer < Sinatra::Base
30
- Rack::Requestash.install
30
+ use Rack::Requestash::AccessLogger
31
31
 
32
32
  get '/' do
33
33
  'Yahtzee!'
@@ -1,49 +1,5 @@
1
- require 'rack/commonlogger'
2
- require 'json'
3
-
4
1
  module Rack
5
- class Requestash
6
- VERSION = "0.0.1"
7
-
8
- def initialize(app, options={})
9
- @app = app
10
-
11
- install if options[:disable_accesslog].nil?
12
- end
13
-
14
- def call(env)
15
- # Simple pass-through, we don't need to mess with the request
16
- @app.call(env)
17
- end
18
-
19
- # Install the Rack::CommonLogger monkeypatch
20
- def install
21
- Rack::CommonLogger.class_eval do
22
-
23
- alias_method :original_log, :log
24
-
25
- def log(env, status, header, began_at)
26
- logger = @logger || env['rack.errors']
27
-
28
- blob = {
29
- :length => header['Content-Length'] || 0,
30
- :code => status.to_s[0 .. 3],
31
- :version => env['HTTP_VERSION'],
32
- :method => env['REQUEST_METHOD'],
33
- :duration => (Time.now - began_at),
34
- :query => env["QUERY_STRING"],
35
- :path => env['PATH_INFO'],
36
- :ip => env['HTTP_X_FORWARDED_FOR'] || env['REMOTE_ADDR'],
37
- :user => env['REMOTE_USER'],
38
- :timestamp => Time.now.utc.iso8601
39
- }
40
-
41
- if logger
42
- logger.write(blob.to_json)
43
- logger.write("\n")
44
- end
45
- end
46
- end
47
- end
2
+ module Requestash
3
+ VERSION = '0.2.0'
48
4
  end
49
5
  end
@@ -0,0 +1,51 @@
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
+
@@ -0,0 +1,28 @@
1
+ require 'log4r'
2
+ require 'json'
3
+
4
+ module Rack
5
+ module Requestash
6
+ module Log4r
7
+ class Formatter < ::Log4r::Formatter
8
+ # Generate a JSON formatted log message, e.g.
9
+ #
10
+ # {"type":"log","event":{"level":"WARN","tracer":[stacktrace],
11
+ # "message":"logmessage"}}
12
+ #
13
+ # @param [Log4::LogEvent] event
14
+ # @return [String] JSOn encoded output
15
+ def format(event)
16
+ {
17
+ :type => 'log',
18
+ :event => {
19
+ :tracer => event.tracer,
20
+ :level => ::Log4r::LNAMES[event.level],
21
+ :message => event.data
22
+ }
23
+ }.to_json
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'rack/requestash/log4r'
3
+ require 'json'
4
+
5
+ describe Rack::Requestash::Log4r::Formatter do
6
+ subject(:formatter) { described_class.new }
7
+
8
+ before :all do
9
+ # Ensure Log4r::LNAMES gets seeded
10
+ Log4r::RootLogger.instance
11
+ end
12
+
13
+ describe '#format' do
14
+ let(:logevent) { ::Log4r::LogEvent.new(level, logger, tracer, data) }
15
+ let(:logger) do
16
+ logger = double('RSpec Logger')
17
+ logger.stub(:name => 'rspec')
18
+ logger.stub(:fullname => 'rspec fullname')
19
+ logger
20
+ end
21
+ let(:tracer) { [] }
22
+
23
+ subject(:log) { formatter.format(logevent) }
24
+
25
+ context 'with an WARN log level' do
26
+ # level 3 is "warn" in log4r
27
+ let(:level) { 3 }
28
+ let(:data) { "rspec log data" }
29
+
30
+ it { should be_instance_of String }
31
+
32
+ it "should be JSON" do
33
+ expect {
34
+ JSON.parse(log)
35
+ }.not_to raise_error
36
+ end
37
+
38
+ context "the JSON log output" do
39
+ subject(:output) { JSON.parse(log) }
40
+
41
+ it { should have_key 'type' }
42
+ it 'should have "log" as the type' do
43
+ expect(output['type']).to eql('log')
44
+ end
45
+ it { should have_key 'event' }
46
+
47
+ context "it's event block" do
48
+ subject(:event) { output['event'] }
49
+
50
+ it { should be_instance_of Hash }
51
+
52
+ it 'should have the tracer' do
53
+ expect(event['tracer']).to eql(tracer)
54
+ end
55
+
56
+ it 'should have the textual log level' do
57
+ expect(event['level']).to eql('WARN')
58
+ end
59
+
60
+ it 'should have the "data" from the event as a "message"' do
61
+ expect(event['message']).to eql(data)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ 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.0.1
4
+ version: 0.2.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-08-19 00:00:00.000000000 Z
12
+ date: 2013-08-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -73,7 +73,10 @@ files:
73
73
  - README.md
74
74
  - Rakefile
75
75
  - lib/rack/requestash.rb
76
+ - lib/rack/requestash/accesslogger.rb
77
+ - lib/rack/requestash/log4r.rb
76
78
  - rack-requestash.gemspec
79
+ - spec/log4r_spec.rb
77
80
  - spec/spec_helper.rb
78
81
  homepage: https://github.com/lookout/rack-requestash
79
82
  licenses:
@@ -90,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
90
93
  version: '0'
91
94
  segments:
92
95
  - 0
93
- hash: 123322180174946479
96
+ hash: 2702330830701692411
94
97
  required_rubygems_version: !ruby/object:Gem::Requirement
95
98
  none: false
96
99
  requirements:
@@ -99,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
102
  version: '0'
100
103
  segments:
101
104
  - 0
102
- hash: 123322180174946479
105
+ hash: 2702330830701692411
103
106
  requirements: []
104
107
  rubyforge_project:
105
108
  rubygems_version: 1.8.25
@@ -108,4 +111,5 @@ specification_version: 3
108
111
  summary: A simple gem for overwriting outputting JSON formatted access logs from Rack
109
112
  apps
110
113
  test_files:
114
+ - spec/log4r_spec.rb
111
115
  - spec/spec_helper.rb