lograge 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -52,7 +52,7 @@ gem "lograge"
52
52
 
53
53
  Enable it for the relevant environments, e.g. production:
54
54
 
55
- ```
55
+ ```ruby
56
56
  # config/environments/production.rb
57
57
  MyApp::Application.configure do
58
58
  config.lograge.enabled = true
@@ -61,7 +61,7 @@ end
61
61
 
62
62
  You can also add a hook for own custom data
63
63
 
64
- ```
64
+ ```ruby
65
65
  # config/environments/staging.rb
66
66
  MyApp::Application.configure do
67
67
  config.lograge.enabled = true
@@ -79,7 +79,7 @@ Lograge supports multiple output formats. The most common is the default
79
79
  lograge format described above. Alternatively, you can also generate JSON
80
80
  logs in the json_event format used by [Logstash](http://logstash.net/).
81
81
 
82
- ```
82
+ ```ruby
83
83
  # config/environments/production.rb
84
84
  MyApp::Application.configure do
85
85
  config.lograge.log_format = :logstash
@@ -161,6 +161,7 @@ that fits in with the general spirit of the log output generated by Lograge.
161
161
 
162
162
  **Changes**
163
163
 
164
+ * Add support for Graylog2 events (Lennart Koopmann, http://github.com/lennartkoopmann)
164
165
  * Add support for Logstash events (Holger Just, http://github.com/meineerde)
165
166
  * Fix for Rails 3.2.9
166
167
  * Use keys everywhere (Curt Michols, http://github.com/asenchi)
@@ -43,6 +43,20 @@ module Lograge
43
43
  event.to_json
44
44
  end
45
45
 
46
+ def process_action_graylog2(data)
47
+ # Cloning because we don't want to mess with the original when mutating keys.
48
+ my = data.clone
49
+
50
+ base = {
51
+ :short_message => "[#{my[:status]}] #{my[:method]} #{my[:path]} (#{my[:controller]}##{my[:action]})"
52
+ }
53
+
54
+ # Add underscore to every key to follow GELF additional field syntax.
55
+ my.keys.each { |k| my["_#{k}".to_sym] = my[k]; my.delete(k) }
56
+
57
+ my.merge(base)
58
+ end
59
+
46
60
  def redirect_to(event)
47
61
  Thread.current[:lograge_location] = event.payload[:location]
48
62
  end
@@ -1,3 +1,3 @@
1
1
  module Lograge
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -196,6 +196,107 @@ describe Lograge::RequestLogSubscriber do
196
196
  end
197
197
  end
198
198
 
199
+ describe "when processing an action with graylog2 output" do
200
+ before do
201
+ Lograge::log_format = :graylog2
202
+ end
203
+
204
+ it "should include the URL in the log output" do
205
+ subscriber.process_action(event)
206
+ log_output.string.should include(':_path=>"/home"')
207
+ end
208
+
209
+ it "should start include the HTTP method" do
210
+ subscriber.process_action(event)
211
+ log_output.string.should include(':_method=>"GET"')
212
+ end
213
+
214
+ it "should include the status code" do
215
+ subscriber.process_action(event)
216
+ log_output.string.should include(':_status=>200') end
217
+
218
+ it "should include the controller and action" do
219
+ subscriber.process_action(event)
220
+ log_output.string.should include(':_controller=>"home"')
221
+ log_output.string.should include(':_action=>"index"')
222
+ end
223
+
224
+ it "should include the duration" do
225
+ subscriber.process_action(event)
226
+ log_output.string.should =~ /:_duration=>\d+\.\d{0,2}/
227
+ end
228
+
229
+ it "should include the view rendering time" do
230
+ subscriber.process_action(event)
231
+ log_output.string.should include(':_view=>0.01')
232
+ end
233
+
234
+ it "should include the database rendering time" do
235
+ subscriber.process_action(event)
236
+ log_output.string.should include(':_db=>0.02')
237
+ end
238
+
239
+ it "should add a 500 status when an exception occurred" do
240
+ event.payload[:status] = nil
241
+ event.payload[:exception] = ['AbstractController::ActionNotFound', 'Route not found']
242
+ subscriber.process_action(event)
243
+ log_output.string.should include(':_status=>500')
244
+ log_output.string.should include(':_error=>"AbstractController::ActionNotFound:Route not found"')
245
+ end
246
+
247
+ it "should return an unknown status when no status or exception is found" do
248
+ event.payload[:status] = nil
249
+ event.payload[:exception] = nil
250
+ subscriber.process_action(event)
251
+ log_output.string.should include(':_status=>0')
252
+ end
253
+
254
+ describe "with a redirect" do
255
+ before do
256
+ Thread.current[:lograge_location] = "http://www.example.com"
257
+ end
258
+
259
+ it "should add the location to the log line" do
260
+ subscriber.process_action(event)
261
+ log_output.string.should include(':_location=>"http://www.example.com"')
262
+ end
263
+
264
+ it "should remove the thread local variable" do
265
+ subscriber.process_action(event)
266
+ Thread.current[:lograge_location].should == nil
267
+ end
268
+ end
269
+
270
+ it "should not include a location by default" do
271
+ subscriber.process_action(event)
272
+ log_output.string.should_not =~ /"location":/
273
+ end
274
+ end
275
+
276
+ describe "with custom_options configured for graylog2 output" do
277
+ before do
278
+ Lograge::log_format = :graylog2
279
+ end
280
+
281
+ it "should combine the hash properly for the output" do
282
+ Lograge.custom_options = {:data => "value"}
283
+ subscriber.process_action(event)
284
+ log_output.string.should include(':_data=>"value"')
285
+ end
286
+
287
+ it "should combine the output of a lambda properly" do
288
+ Lograge.custom_options = lambda {|event| {:data => "value"}}
289
+ subscriber.process_action(event)
290
+ log_output.string.should include(':_data=>"value"')
291
+ end
292
+
293
+ it "should work if the method returns nil" do
294
+ Lograge.custom_options = lambda {|event| nil}
295
+ subscriber.process_action(event)
296
+ log_output.string.should be_present
297
+ end
298
+ end
299
+
199
300
  describe "with custom_options configured for lograge output" do
200
301
  before do
201
302
  Lograge::log_format = :lograge
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lograge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
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: 2012-12-10 00:00:00.000000000 Z
12
+ date: 2013-02-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec