lograge 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,261 +0,0 @@
1
- require 'spec_helper'
2
- require 'lograge'
3
- require 'lograge/log_subscriber'
4
- require 'active_support/notifications'
5
- require 'active_support/core_ext/string'
6
- require 'logger'
7
-
8
- describe Lograge::RequestLogSubscriber do
9
- let(:log_output) {StringIO.new}
10
- let(:logger) {
11
- Logger.new(log_output).tap {|logger| logger.formatter = ->(_, _, _, msg) { msg } }
12
- }
13
-
14
- let(:subscriber) {Lograge::RequestLogSubscriber.new}
15
- let(:event) {
16
- ActiveSupport::Notifications::Event.new(
17
- 'process_action.action_controller', Time.now, Time.now, 2, {
18
- status: 200, format: 'application/json', method: 'GET', path: '/home?foo=bar', params: {
19
- 'controller' => 'home', 'action' => 'index', 'foo' => 'bar'
20
- }, db_runtime: 0.02, view_runtime: 0.01
21
- }
22
- )
23
- }
24
- let(:redirect) {
25
- ActiveSupport::Notifications::Event.new(
26
- 'redirect_to.action_controller', Time.now, Time.now, 1, location: 'http://example.com', status: 302
27
- )
28
- }
29
-
30
- before { Lograge.logger = logger }
31
-
32
- describe "with custom_options configured for cee output" do
33
- before do
34
- Lograge::formatter = ->(data) { "My test: #{data}" }
35
- end
36
-
37
- it "should combine the hash properly for the output" do
38
- Lograge.custom_options = {:data => "value"}
39
- subscriber.process_action(event)
40
- log_output.string.should =~ /^My test: {.*:data=>"value"/
41
- end
42
- it "should combine the output of a lambda properly" do
43
- Lograge.custom_options = lambda {|event| {:data => "value"}}
44
- subscriber.process_action(event)
45
- log_output.string.should =~ /^My test: {.*:data=>"value"/
46
- end
47
- it "should work if the method returns nil" do
48
- Lograge.custom_options = lambda {|event| nil}
49
- subscriber.process_action(event)
50
- log_output.string.should be_present
51
- end
52
- end
53
-
54
- describe "when processing a redirect" do
55
- it "should store the location in a thread local variable" do
56
- subscriber.redirect_to(redirect)
57
- Thread.current[:lograge_location].should == "http://example.com"
58
- end
59
- end
60
-
61
- describe "when processing an action with lograge output" do
62
- before do
63
- Lograge.formatter = Lograge::Formatters::KeyValue.new
64
- end
65
-
66
- it "should include the URL in the log output" do
67
- subscriber.process_action(event)
68
- log_output.string.should include('/home')
69
- end
70
-
71
- it "should not include the query string in the url" do
72
- subscriber.process_action(event)
73
- log_output.string.should_not include('?foo=bar')
74
- end
75
-
76
- it "should start the log line with the HTTP method" do
77
- subscriber.process_action(event)
78
- log_output.string.starts_with?('method=GET ').should == true
79
- end
80
-
81
- it "should include the status code" do
82
- subscriber.process_action(event)
83
- log_output.string.should include('status=200 ')
84
- end
85
-
86
- it "should include the controller and action" do
87
- subscriber.process_action(event)
88
- log_output.string.should include('controller=home action=index')
89
- end
90
-
91
- it "should include the duration" do
92
- subscriber.process_action(event)
93
- log_output.string.should =~ /duration=[\.0-9]{4,4} /
94
- end
95
-
96
- it "should include the view rendering time" do
97
- subscriber.process_action(event)
98
- log_output.string.should =~ /view=0.01 /
99
- end
100
-
101
- it "should include the database rendering time" do
102
- subscriber.process_action(event)
103
- log_output.string.should =~ /db=0.02/
104
- end
105
-
106
- it "should add a 500 status when an exception occurred" do
107
- event.payload[:status] = nil
108
- event.payload[:exception] = ['AbstractController::ActionNotFound', 'Route not found']
109
- subscriber.process_action(event)
110
- log_output.string.should =~ /status=500 /
111
- log_output.string.should =~ /error='AbstractController::ActionNotFound:Route not found' /
112
- end
113
-
114
- it "should return an unknown status when no status or exception is found" do
115
- event.payload[:status] = nil
116
- event.payload[:exception] = nil
117
- subscriber.process_action(event)
118
- log_output.string.should =~ /status=0 /
119
- end
120
-
121
- describe "with a redirect" do
122
- before do
123
- Thread.current[:lograge_location] = "http://www.example.com"
124
- end
125
-
126
- it "should add the location to the log line" do
127
- subscriber.process_action(event)
128
- log_output.string.should =~ %r{ location=http://www.example.com}
129
- end
130
-
131
- it "should remove the thread local variable" do
132
- subscriber.process_action(event)
133
- Thread.current[:lograge_location].should == nil
134
- end
135
- end
136
-
137
- it "should not include a location by default" do
138
- subscriber.process_action(event)
139
- log_output.string.should_not =~ /location=/
140
- end
141
- end
142
-
143
- describe "with custom_options configured for lograge output" do
144
- before do
145
- Lograge.formatter = Lograge::Formatters::KeyValue.new
146
- end
147
-
148
- it "should combine the hash properly for the output" do
149
- Lograge.custom_options = {:data => "value"}
150
- subscriber.process_action(event)
151
- log_output.string.should =~ / data=value/
152
- end
153
- it "should combine the output of a lambda properly" do
154
- Lograge.custom_options = lambda {|event| {:data => "value"}}
155
- subscriber.process_action(event)
156
- log_output.string.should =~ / data=value/
157
- end
158
- it "should work if the method returns nil" do
159
- Lograge.custom_options = lambda {|event| nil}
160
- subscriber.process_action(event)
161
- log_output.string.should be_present
162
- end
163
- end
164
-
165
- describe "with before_format configured for lograge output" do
166
- before do
167
- Lograge.formatter = Lograge::Formatters::KeyValue.new
168
- Lograge.before_format = nil
169
- end
170
-
171
- it "should output correctly" do
172
- Lograge.before_format = lambda { |data, payload|
173
- Hash[*data.first].merge(Hash[*payload.first])
174
- }
175
- subscriber.process_action(event)
176
- log_output.string.should include("method=GET")
177
- log_output.string.should include("status=200")
178
- end
179
- it "should work if the method returns nil" do
180
- Lograge.before_format = lambda {|data, payload| nil}
181
- subscriber.process_action(event)
182
- log_output.string.should be_present
183
- end
184
- end
185
-
186
- describe "with ignore configured" do
187
- before do
188
- # Lograge::log_format = :lograge
189
- Lograge::ignore_nothing # clear the old ignores before each test
190
- end
191
-
192
- it "should not log ignored controller actions given a single ignored action" do
193
- Lograge.ignore_actions 'home#index'
194
- subscriber.process_action(event)
195
- log_output.string.should be_blank
196
- end
197
-
198
- it "should not log ignored controller actions given a single ignored action after a custom ignore" do
199
- Lograge.ignore(lambda {|event| false})
200
- Lograge.ignore_actions 'home#index'
201
- subscriber.process_action(event)
202
- log_output.string.should be_blank
203
- end
204
-
205
- it "should log non-ignored controller actions given a single ignored action" do
206
- Lograge.ignore_actions 'foo#bar'
207
- subscriber.process_action(event)
208
- log_output.string.should_not be_blank
209
- end
210
-
211
- it "should not log ignored controller actions given multiple ignored actions" do
212
- Lograge.ignore_actions ['foo#bar', 'home#index', 'bar#foo']
213
- subscriber.process_action(event)
214
- log_output.string.should be_blank
215
- end
216
-
217
- it "should log non-ignored controller actions given multiple ignored actions" do
218
- Lograge.ignore_actions ['foo#bar', 'bar#foo']
219
- subscriber.process_action(event)
220
- log_output.string.should_not be_blank
221
- end
222
-
223
- it "should not log ignored events" do
224
- Lograge.ignore(lambda do |event|
225
- 'GET' == event.payload[:method]
226
- end)
227
- subscriber.process_action(event)
228
- log_output.string.should be_blank
229
- end
230
-
231
- it "should log non-ignored events" do
232
- Lograge.ignore(lambda do |event|
233
- 'foo' == event.payload[:method]
234
- end)
235
- subscriber.process_action(event)
236
- log_output.string.should_not be_blank
237
- end
238
-
239
- it "should not choke on nil ignore_actions input" do
240
- Lograge.ignore_actions nil
241
- subscriber.process_action(event)
242
- log_output.string.should_not be_blank
243
- end
244
-
245
- it "should not choke on nil ignore input" do
246
- Lograge.ignore nil
247
- subscriber.process_action(event)
248
- log_output.string.should_not be_blank
249
- end
250
- end
251
-
252
- it "should fallback to ActiveSupport's logger if one isn't configured" do
253
- Lograge.formatter = Lograge::Formatters::KeyValue.new
254
- Lograge.logger = nil
255
- ActiveSupport::LogSubscriber.logger = logger
256
-
257
- subscriber.process_action(event)
258
-
259
- log_output.string.should be_present
260
- end
261
- end
data/spec/lograge_spec.rb DELETED
@@ -1,84 +0,0 @@
1
- require 'spec_helper'
2
- require 'lograge'
3
- require 'active_support/notifications'
4
- require 'active_support/core_ext/string'
5
- require 'active_support/log_subscriber'
6
- require 'action_controller/log_subscriber'
7
- require 'action_view/log_subscriber'
8
-
9
- describe Lograge do
10
- describe "when removing Rails' log subscribers" do
11
- after do
12
- ActionController::LogSubscriber.attach_to :action_controller
13
- ActionView::LogSubscriber.attach_to :action_view
14
- end
15
-
16
- it "should remove subscribers for controller events" do
17
- expect {
18
- Lograge.remove_existing_log_subscriptions
19
- }.to change {
20
- ActiveSupport::Notifications.notifier.listeners_for('process_action.action_controller')
21
- }
22
- end
23
-
24
- it "should remove subscribers for all events" do
25
- expect {
26
- Lograge.remove_existing_log_subscriptions
27
- }.to change {
28
- ActiveSupport::Notifications.notifier.listeners_for('render_template.action_view')
29
- }
30
- end
31
-
32
- it "shouldn't remove subscribers that aren't from Rails" do
33
- blk = -> {}
34
- ActiveSupport::Notifications.subscribe("process_action.action_controller", &blk)
35
- Lograge.remove_existing_log_subscriptions
36
- listeners = ActiveSupport::Notifications.notifier.listeners_for('process_action.action_controller')
37
- listeners.size.should > 0
38
- end
39
- end
40
-
41
- describe 'deprecated log_format interpreter' do
42
- let(:app_config) do
43
- double(config:
44
- ActiveSupport::OrderedOptions.new.tap do |config|
45
- config.action_dispatch = double(rack_cache: false)
46
- config.lograge = ActiveSupport::OrderedOptions.new
47
- config.lograge.log_format = format
48
- end
49
- )
50
- end
51
- before { ActiveSupport::Deprecation.silence { Lograge.setup(app_config) } }
52
- subject { Lograge.formatter }
53
-
54
- context ':cee' do
55
- let(:format) { :cee }
56
- it { should be_instance_of(Lograge::Formatters::Cee) }
57
- end
58
-
59
- context ':raw' do
60
- let(:format) { :raw }
61
- it { should be_instance_of(Lograge::Formatters::Raw) }
62
- end
63
-
64
- context ':logstash' do
65
- let(:format) { :logstash }
66
- it { should be_instance_of(Lograge::Formatters::Logstash) }
67
- end
68
-
69
- context ':graylog2' do
70
- let(:format) { :graylog2 }
71
- it { should be_instance_of(Lograge::Formatters::Graylog2) }
72
- end
73
-
74
- context ':lograge' do
75
- let(:format) { :lograge }
76
- it { should be_instance_of(Lograge::Formatters::KeyValue) }
77
- end
78
-
79
- context 'default' do
80
- let(:format) { nil }
81
- it { should be_instance_of(Lograge::Formatters::KeyValue) }
82
- end
83
- end
84
- end
data/spec/spec_helper.rb DELETED
@@ -1,14 +0,0 @@
1
- # This file was generated by the `rspec --init` command. Conventionally, all
2
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
- # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
- # loaded once.
5
- #
6
- # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
- require 'action_pack'
8
- require 'support/examples'
9
-
10
- RSpec.configure do |config|
11
- config.treat_symbols_as_metadata_keys_with_true_values = true
12
- config.run_all_when_everything_filtered = true
13
- config.filter_run :focus
14
- end
@@ -1,20 +0,0 @@
1
-
2
- shared_examples_for "a key value formatter" do
3
- let(:payload) do
4
- {
5
- custom: 'data',
6
- status: 200,
7
- method: 'GET',
8
- path: '/',
9
- controller: 'welcome',
10
- action: 'index'
11
- }
12
- end
13
-
14
- subject { described_class.new.call(payload) }
15
-
16
- it { should include('method=GET') }
17
- it { should include('path=/') }
18
- it { should include('status=200') }
19
- it { should include('custom=data') }
20
- end