lograge 0.2.2 → 0.3.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/.travis.yml CHANGED
@@ -2,4 +2,5 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
+ - 2.1.0
5
6
  script: bundle exec rspec
data/CHANGES.md ADDED
@@ -0,0 +1,11 @@
1
+ ### 0.3.0
2
+
3
+ * #47: Add formatter for l2met (<https://github.com/BRMatt>)
4
+
5
+ * #55: Update Logstash formatter for Logstash 1.2 format (<https://github.com/msaffitz>)
6
+
7
+ * #56: Add JSON formatter (<https://github.com/i0rek>)
8
+
9
+ * #59: Add `before_format` hook (<https://github.com/i0rek>)
10
+
11
+ * #60: Add Ruby 2.1.0 for testing on Travis CI (<https://github.com/salimane>)
data/README.md CHANGED
@@ -75,6 +75,19 @@ MyApp::Application.configure do
75
75
  end
76
76
  ```
77
77
 
78
+ Or you can add a timestamp:
79
+
80
+ ```ruby
81
+ MyApp::Application.configure do
82
+ config.lograge.enabled = true
83
+
84
+ # add time to lograge
85
+ config.lograge.custom_options = lambda do |event|
86
+ {:time => event.time}
87
+ end
88
+ end
89
+ ```
90
+
78
91
  You can then add custom variables to the event to be used in custom_options
79
92
 
80
93
  ```ruby
@@ -0,0 +1,10 @@
1
+ require 'json'
2
+ module Lograge
3
+ module Formatters
4
+ class Json
5
+ def call(data)
6
+ ::JSON.dump(data)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -7,23 +7,32 @@ module Lograge
7
7
  ]
8
8
 
9
9
  def call(data)
10
- fields = LOGRAGE_FIELDS
11
- fields += (data.keys - LOGRAGE_FIELDS)
10
+ fields = fields_to_display(data)
12
11
 
13
12
  event = fields.inject([]) do |message, key|
14
13
  next message unless data.has_key?(key)
15
- # Exactly preserve the previous output
16
- # Parsing this can be ambigious if the error messages contains
17
- # a single quote
18
- data[key] = "'#{data[key]}'" if key == :error
19
- # Ensure that we always have exactly two decimals
20
- data[key] = "%.2f" % data[key] if data[key].is_a? Float
21
14
 
22
- message << "#{key}=#{data[key]}"
15
+ message << format(key, data[key])
23
16
  message
24
17
  end
25
18
  event.join(" ")
26
19
  end
20
+
21
+ def fields_to_display(data)
22
+ LOGRAGE_FIELDS + (data.keys - LOGRAGE_FIELDS)
23
+ end
24
+
25
+ def format(key, value)
26
+ # Exactly preserve the previous output
27
+ # Parsing this can be ambigious if the error messages contains
28
+ # a single quote
29
+ value = "'#{value}'" if key == :error
30
+
31
+ # Ensure that we always have exactly two decimals
32
+ value = "%.2f" % value if value.is_a? Float
33
+
34
+ "#{key}=#{value}"
35
+ end
27
36
  end
28
37
  end
29
- end
38
+ end
@@ -0,0 +1,38 @@
1
+ require 'lograge/formatters/key_value'
2
+
3
+ module Lograge
4
+ module Formatters
5
+ class L2met < KeyValue
6
+ L2MET_FIELDS = [
7
+ :method, :path, :format, :source, :status, :error,
8
+ :duration, :view, :db, :location
9
+ ]
10
+
11
+ def call(data)
12
+ super(modify_payload(data))
13
+ end
14
+
15
+ def format(key, value)
16
+ key = "measure#page.#{key}" if value.kind_of?(Float)
17
+
18
+ super(key, value)
19
+ end
20
+
21
+ def fields_to_display(data)
22
+ L2MET_FIELDS + (data.keys - L2MET_FIELDS) - [:controller, :action]
23
+ end
24
+
25
+ def modify_payload(data)
26
+ if data[:controller] && data[:action]
27
+ data[:source] = source_field(data)
28
+ end
29
+
30
+ data
31
+ end
32
+
33
+ def source_field(data)
34
+ "#{data[:controller].to_s.gsub('/','-')}:#{data[:action]}"
35
+ end
36
+ end
37
+ end
38
+ end
@@ -3,7 +3,8 @@ module Lograge
3
3
  class Logstash
4
4
  def call(data)
5
5
  load_dependencies
6
- event = LogStash::Event.new("@fields" => data)
6
+ event = LogStash::Event.new(data)
7
+
7
8
  event.message = "[#{data[:status]}] #{data[:method]} #{data[:path]} (#{data[:controller]}##{data[:action]})"
8
9
  event.to_json
9
10
  end
@@ -16,4 +17,4 @@ module Lograge
16
17
  end
17
18
  end
18
19
  end
19
- end
20
+ end
@@ -7,7 +7,7 @@ module Lograge
7
7
  class RequestLogSubscriber < ActiveSupport::LogSubscriber
8
8
  def process_action(event)
9
9
  return if Lograge.ignore?(event)
10
-
10
+
11
11
  payload = event.payload
12
12
 
13
13
  data = extract_request(payload)
@@ -16,6 +16,7 @@ module Lograge
16
16
  data.merge! location(event)
17
17
  data.merge! custom_options(event)
18
18
 
19
+ data = before_format(data, payload)
19
20
  formatted_message = Lograge.formatter.call(data)
20
21
  logger.send(Lograge.log_level, formatted_message)
21
22
  end
@@ -67,6 +68,10 @@ module Lograge
67
68
  Lograge.custom_options(event) || {}
68
69
  end
69
70
 
71
+ def before_format(data, payload)
72
+ Lograge.before_format(data, payload)
73
+ end
74
+
70
75
  def runtimes(event)
71
76
  {
72
77
  :duration => event.duration,
@@ -1,3 +1,3 @@
1
1
  module Lograge
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/lograge.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require 'lograge/version'
2
2
  require 'lograge/formatters/cee'
3
+ require 'lograge/formatters/json'
3
4
  require 'lograge/formatters/graylog2'
4
5
  require 'lograge/formatters/key_value'
6
+ require 'lograge/formatters/l2met'
5
7
  require 'lograge/formatters/logstash'
6
8
  require 'lograge/formatters/raw'
7
9
  require 'lograge/log_subscriber'
@@ -30,6 +32,18 @@ module Lograge
30
32
  end
31
33
  end
32
34
 
35
+ # Before format allows you to change the structure of the output.
36
+ # You've to pass in something callable
37
+ #
38
+ mattr_writer :before_format
39
+ self.before_format = nil
40
+
41
+ def self.before_format(data, payload)
42
+ result = nil
43
+ result = @@before_format.call(data, payload) if @@before_format
44
+ result || data
45
+ end
46
+
33
47
  # Set conditions for events that should be ignored
34
48
  #
35
49
  # Currently supported formats are:
@@ -38,7 +52,7 @@ module Lograge
38
52
  # - An object that responds to call with an event argument and returns
39
53
  # true iff the event should be ignored.
40
54
  #
41
- # The action ignores are given to 'ignore_actions'. The callable ignores
55
+ # The action ignores are given to 'ignore_actions'. The callable ignores
42
56
  # are given to 'ignore'. Both methods can be called multiple times, which
43
57
  # just adds more ignore conditions to a list that is checked before logging.
44
58
 
@@ -104,6 +118,7 @@ module Lograge
104
118
  Lograge.remove_existing_log_subscriptions
105
119
  Lograge::RequestLogSubscriber.attach_to :action_controller
106
120
  Lograge.custom_options = app.config.lograge.custom_options
121
+ Lograge.before_format = app.config.lograge.before_format
107
122
  Lograge.log_level = app.config.lograge.log_level || :info
108
123
  self.support_deprecated_config(app) # TODO: Remove with version 1.0
109
124
  Lograge.formatter = app.config.lograge.formatter || Lograge::Formatters::KeyValue.new
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ require 'lograge'
3
+
4
+ describe Lograge::Formatters::Json do
5
+ it { expect( subject.call({ custom: 'data' })).to eq('{"custom":"data"}') }
6
+ end
@@ -12,12 +12,11 @@ describe Lograge::Formatters::KeyValue do
12
12
  action: 'index'
13
13
  }
14
14
  end
15
+
15
16
  subject { described_class.new.call(payload) }
16
17
 
17
- it { should include('method=GET') }
18
- it { should include('path=/') }
19
18
  it { should include('controller=welcome') }
20
19
  it { should include('action=index') }
21
- it { should include('status=200') }
22
- it { should include('custom=data') }
23
- end
20
+
21
+ it_behaves_like "a key value formatter"
22
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'lograge'
3
+
4
+ describe Lograge::Formatters::L2met do
5
+ let(:payload) do
6
+ {
7
+ custom: 'data',
8
+ status: 200,
9
+ method: 'GET',
10
+ path: '/',
11
+ controller: 'admin/welcome',
12
+ action: 'index',
13
+ db: 20.00,
14
+ view: 10.00,
15
+ duration: 30.00,
16
+ cache: 40.00
17
+ }
18
+ end
19
+
20
+ it_behaves_like "a key value formatter"
21
+
22
+ subject { described_class.new.call(payload) }
23
+
24
+ it { should include('source=admin-welcome:index') }
25
+ it { should_not include('controller=admin/welcome') }
26
+ it { should_not include('action=index') }
27
+ it { should include('measure#page.duration=30.00') }
28
+ it { should include('measure#page.view=10.00') }
29
+ it { should include('measure#page.db=20.00') }
30
+ it { should include('measure#page.cache=40.00') }
31
+ end
@@ -13,13 +13,10 @@ describe Lograge::Formatters::Logstash do
13
13
  }
14
14
  end
15
15
  subject { described_class.new.call(payload) }
16
- it { should match(/"@source":"unknown"/) }
17
- it { should match(/"@tags":\[\]/) }
18
- it { should match(/"@fields":{/) }
19
16
  it { should match(/"custom":"data"/) }
20
17
  it { should match(/"status":200/) }
21
18
  it { should match(/"method":"GET"/) }
22
19
  it { should match(/"path":"\/"/) }
23
20
  it { should match(/"controller":"welcome"/) }
24
21
  it { should match(/"action":"index"/) }
25
- end
22
+ end
@@ -162,6 +162,27 @@ describe Lograge::RequestLogSubscriber do
162
162
  end
163
163
  end
164
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
+
165
186
  describe "with ignore configured" do
166
187
  before do
167
188
  # Lograge::log_format = :lograge
data/spec/spec_helper.rb CHANGED
@@ -5,6 +5,7 @@
5
5
  #
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
7
  require 'action_pack'
8
+ require 'support/examples'
8
9
 
9
10
  RSpec.configure do |config|
10
11
  config.treat_symbols_as_metadata_keys_with_true_values = true
@@ -0,0 +1,20 @@
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
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.2.2
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-09-17 00:00:00.000000000 Z
12
+ date: 2014-03-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -101,6 +101,7 @@ files:
101
101
  - .gitignore
102
102
  - .rspec
103
103
  - .travis.yml
104
+ - CHANGES.md
104
105
  - Gemfile
105
106
  - Guardfile
106
107
  - README.md
@@ -108,7 +109,9 @@ files:
108
109
  - lib/lograge.rb
109
110
  - lib/lograge/formatters/cee.rb
110
111
  - lib/lograge/formatters/graylog2.rb
112
+ - lib/lograge/formatters/json.rb
111
113
  - lib/lograge/formatters/key_value.rb
114
+ - lib/lograge/formatters/l2met.rb
112
115
  - lib/lograge/formatters/logstash.rb
113
116
  - lib/lograge/formatters/raw.rb
114
117
  - lib/lograge/log_subscriber.rb
@@ -118,12 +121,15 @@ files:
118
121
  - lograge.gemspec
119
122
  - spec/formatters/cee_spec.rb
120
123
  - spec/formatters/graylog2_spec.rb
124
+ - spec/formatters/json_spec.rb
121
125
  - spec/formatters/key_value_spec.rb
126
+ - spec/formatters/l2met_spec.rb
122
127
  - spec/formatters/logstash_spec.rb
123
128
  - spec/formatters/raw_spec.rb
124
129
  - spec/lograge_logsubscriber_spec.rb
125
130
  - spec/lograge_spec.rb
126
131
  - spec/spec_helper.rb
132
+ - spec/support/examples.rb
127
133
  homepage: https://github.com/roidrage/lograge
128
134
  licenses: []
129
135
  post_install_message:
@@ -151,10 +157,13 @@ summary: Tame Rails' multi-line logging into a single line per request
151
157
  test_files:
152
158
  - spec/formatters/cee_spec.rb
153
159
  - spec/formatters/graylog2_spec.rb
160
+ - spec/formatters/json_spec.rb
154
161
  - spec/formatters/key_value_spec.rb
162
+ - spec/formatters/l2met_spec.rb
155
163
  - spec/formatters/logstash_spec.rb
156
164
  - spec/formatters/raw_spec.rb
157
165
  - spec/lograge_logsubscriber_spec.rb
158
166
  - spec/lograge_spec.rb
159
167
  - spec/spec_helper.rb
168
+ - spec/support/examples.rb
160
169
  has_rdoc: