lograge 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e83952d334c15f42c841f0ed45c8e1538e795ab
4
+ data.tar.gz: 581be0bc6a70dfa1abaad4bbefef0a80115f837c
5
+ SHA512:
6
+ metadata.gz: f21494756b287b5d0c95594f9a285b70e2af281f9b25a19f4e2c90a4656058cadc869337f9671002af5403ffc2331b8a3e1230ba3363e0c7b861e7569f8fb30c
7
+ data.tar.gz: 09f4b1e38c61db6caff08c51019ffdb982f2b7ca536ea5be7edbbd9a113fb48ca3dbb041603737cedaf810cbcb85c48b9d9a166fd330de7ca0639523d53ccb3d
@@ -6,4 +6,4 @@ module Lograge
6
6
  end
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -3,17 +3,28 @@ module Lograge
3
3
  class Graylog2
4
4
  def call(data)
5
5
  # Cloning because we don't want to mess with the original when mutating keys.
6
- my = data.clone
6
+ data_clone = data.clone
7
7
 
8
8
  base = {
9
- :short_message => "[#{my[:status]}] #{my[:method]} #{my[:path]} (#{my[:controller]}##{my[:action]})"
9
+ short_message: short_message(data_clone)
10
10
  }
11
11
 
12
12
  # Add underscore to every key to follow GELF additional field syntax.
13
- my.keys.each { |k| my["_#{k}".to_sym] = my[k]; my.delete(k) }
13
+ data_clone.keys.each do |key|
14
+ data_clone[underscore_prefix(key)] = data_clone[key]
15
+ data_clone.delete(key)
16
+ end
14
17
 
15
- my.merge(base)
18
+ data_clone.merge(base)
19
+ end
20
+
21
+ def underscore_prefix(key)
22
+ "_#{key}".to_sym
23
+ end
24
+
25
+ def short_message(data)
26
+ "[#{data[:status]}] #{data[:method]} #{data[:path]} (#{data[:controller]}##{data[:action]})"
16
27
  end
17
28
  end
18
29
  end
19
- end
30
+ end
@@ -1,35 +1,27 @@
1
1
  module Lograge
2
2
  module Formatters
3
3
  class KeyValue
4
- LOGRAGE_FIELDS = [
5
- :method, :path, :format, :controller, :action, :status, :error,
6
- :duration, :view, :db, :location
7
- ]
8
-
9
4
  def call(data)
10
5
  fields = fields_to_display(data)
11
6
 
12
- event = fields.inject([]) do |message, key|
13
- next message unless data.has_key?(key)
14
-
15
- message << format(key, data[key])
16
- message
17
- end
18
- event.join(" ")
7
+ event = fields.map { |key| format(key, data[key]) }
8
+ event.join(' ')
19
9
  end
20
10
 
21
11
  def fields_to_display(data)
22
- LOGRAGE_FIELDS + (data.keys - LOGRAGE_FIELDS)
12
+ data.keys
23
13
  end
24
14
 
25
15
  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
16
+ if key == :error
17
+ # Exactly preserve the previous output
18
+ # Parsing this can be ambigious if the error messages contains
19
+ # a single quote
20
+ value = "'#{value}'"
21
+ else
22
+ # Ensure that we always have exactly two decimals
23
+ value = Kernel.format('%.2f', value) if value.is_a? Float
24
+ end
33
25
 
34
26
  "#{key}=#{value}"
35
27
  end
@@ -13,7 +13,7 @@ module Lograge
13
13
  end
14
14
 
15
15
  def format(key, value)
16
- key = "measure#page.#{key}" if value.kind_of?(Float)
16
+ key = "measure#page.#{key}" if value.is_a?(Float)
17
17
 
18
18
  super(key, value)
19
19
  end
@@ -23,15 +23,13 @@ module Lograge
23
23
  end
24
24
 
25
25
  def modify_payload(data)
26
- if data[:controller] && data[:action]
27
- data[:source] = source_field(data)
28
- end
26
+ data[:source] = source_field(data) if data[:controller] && data[:action]
29
27
 
30
28
  data
31
29
  end
32
30
 
33
31
  def source_field(data)
34
- "#{data[:controller].to_s.gsub('/','-')}:#{data[:action]}"
32
+ "#{data[:controller].to_s.gsub('/', '-')}:#{data[:action]}"
35
33
  end
36
34
  end
37
35
  end
@@ -0,0 +1,18 @@
1
+ module Lograge
2
+ module Formatters
3
+ class Lines
4
+ def call(data)
5
+ load_dependencies
6
+
7
+ ::Lines.dump(data)
8
+ end
9
+
10
+ def load_dependencies
11
+ require 'lines'
12
+ rescue LoadError
13
+ puts 'You need to install the lines gem to use this output.'
14
+ raise
15
+ end
16
+ end
17
+ end
18
+ end
@@ -4,15 +4,15 @@ module Lograge
4
4
  def call(data)
5
5
  load_dependencies
6
6
  event = LogStash::Event.new(data)
7
-
7
+
8
8
  event.message = "[#{data[:status]}] #{data[:method]} #{data[:path]} (#{data[:controller]}##{data[:action]})"
9
9
  event.to_json
10
10
  end
11
11
 
12
12
  def load_dependencies
13
- require "logstash-event"
13
+ require 'logstash-event'
14
14
  rescue LoadError
15
- puts "You need to install the logstash-event gem to use the logstash output."
15
+ puts 'You need to install the logstash-event gem to use the logstash output.'
16
16
  raise
17
17
  end
18
18
  end
@@ -6,4 +6,4 @@ module Lograge
6
6
  end
7
7
  end
8
8
  end
9
- end
9
+ end
@@ -11,10 +11,10 @@ module Lograge
11
11
  payload = event.payload
12
12
 
13
13
  data = extract_request(payload)
14
- data.merge! extract_status(payload)
15
- data.merge! runtimes(event)
16
- data.merge! location(event)
17
- data.merge! custom_options(event)
14
+ extract_status(data, payload)
15
+ runtimes(data, event)
16
+ location(data)
17
+ custom_options(data, event)
18
18
 
19
19
  data = before_format(data, payload)
20
20
  formatted_message = Lograge.formatter.call(data)
@@ -26,70 +26,71 @@ module Lograge
26
26
  end
27
27
 
28
28
  def logger
29
- Lograge.logger.presence or super
29
+ Lograge.logger.presence || super
30
30
  end
31
31
 
32
32
  private
33
33
 
34
34
  def extract_request(payload)
35
35
  {
36
- :method => payload[:method],
37
- :path => extract_path(payload),
38
- :format => extract_format(payload),
39
- :controller => payload[:params]['controller'],
40
- :action => payload[:params]['action']
36
+ method: payload[:method],
37
+ path: extract_path(payload),
38
+ format: extract_format(payload),
39
+ controller: payload[:params]['controller'],
40
+ action: payload[:params]['action']
41
41
  }
42
42
  end
43
43
 
44
44
  def extract_path(payload)
45
- payload[:path].split("?").first
45
+ path = payload[:path]
46
+ index = path.index('?')
47
+ index ? path[0, index] : path
46
48
  end
47
49
 
48
- def extract_format(payload)
49
- if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
50
+ if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
51
+ def extract_format(payload)
50
52
  payload[:formats].first
51
- else
53
+ end
54
+ else
55
+ def extract_format(payload)
52
56
  payload[:format]
53
57
  end
54
58
  end
55
59
 
56
- def extract_status(payload)
57
- if payload[:status]
58
- { :status => payload[:status].to_i }
59
- elsif payload[:exception]
60
- exception, message = payload[:exception]
61
- { :status => 500, :error => "#{exception}:#{message}" }
60
+ def extract_status(data, payload)
61
+ if (status = payload[:status])
62
+ data[:status] = status.to_i
63
+ elsif (error = payload[:exception])
64
+ exception, message = error
65
+ data[:status] = 500
66
+ data[:error] = "#{exception}:#{message}"
62
67
  else
63
- { :status => 0 }
68
+ data[:status] = 0
64
69
  end
65
70
  end
66
71
 
67
- def custom_options(event)
68
- Lograge.custom_options(event) || {}
72
+ def custom_options(data, event)
73
+ options = Lograge.custom_options(event)
74
+ data.merge! options if options
69
75
  end
70
76
 
71
77
  def before_format(data, payload)
72
78
  Lograge.before_format(data, payload)
73
79
  end
74
80
 
75
- def runtimes(event)
76
- {
77
- :duration => event.duration,
78
- :view => event.payload[:view_runtime],
79
- :db => event.payload[:db_runtime]
80
- }.inject({}) do |runtimes, (name, runtime)|
81
- runtimes[name] = runtime.to_f.round(2) if runtime
82
- runtimes
83
- end
81
+ def runtimes(data, event)
82
+ payload = event.payload
83
+ data[:duration] = event.duration.to_f.round(2)
84
+ data[:view] = payload[:view_runtime].to_f.round(2) if payload.key?(:view_runtime)
85
+ data[:db] = payload[:db_runtime].to_f.round(2) if payload.key?(:db_runtime)
84
86
  end
85
87
 
86
- def location(event)
87
- if location = Thread.current[:lograge_location]
88
- Thread.current[:lograge_location] = nil
89
- { :location => location }
90
- else
91
- {}
92
- end
88
+ def location(data)
89
+ location = Thread.current[:lograge_location]
90
+ return unless location
91
+
92
+ Thread.current[:lograge_location] = nil
93
+ data[:location] = location
93
94
  end
94
95
  end
95
96
  end
@@ -18,7 +18,7 @@ module Rails
18
18
  end
19
19
 
20
20
  # Overwrites Rails 3.0/3.1 code that logs new requests
21
- def before_dispatch(env)
21
+ def before_dispatch(_env)
22
22
  end
23
23
  end
24
24
  end
@@ -1,3 +1,3 @@
1
1
  module Lograge
2
- VERSION = "0.3.0"
2
+ VERSION = '0.3.1'
3
3
  end
data/lib/lograge.rb CHANGED
@@ -4,6 +4,7 @@ require 'lograge/formatters/json'
4
4
  require 'lograge/formatters/graylog2'
5
5
  require 'lograge/formatters/key_value'
6
6
  require 'lograge/formatters/l2met'
7
+ require 'lograge/formatters/lines'
7
8
  require 'lograge/formatters/logstash'
8
9
  require 'lograge/formatters/raw'
9
10
  require 'lograge/log_subscriber'
@@ -11,9 +12,10 @@ require 'active_support/core_ext/module/attribute_accessors'
11
12
  require 'active_support/core_ext/string/inflections'
12
13
  require 'active_support/ordered_options'
13
14
 
14
-
15
15
  module Lograge
16
- mattr_accessor :logger
16
+ module_function
17
+
18
+ mattr_accessor :logger, :application, :ignore_tests
17
19
 
18
20
  # Custom options that will be appended to log line
19
21
  #
@@ -58,25 +60,25 @@ module Lograge
58
60
 
59
61
  def self.ignore_actions(actions)
60
62
  ignore(lambda do |event|
61
- params = event.payload[:params]
62
- Array(actions).include?("#{params['controller']}##{params['action']}")
63
- end)
63
+ params = event.payload[:params]
64
+ Array(actions).include?("#{params['controller']}##{params['action']}")
65
+ end)
64
66
  end
65
67
 
66
- def self.ignore_tests
67
- @@ignore_tests ||= []
68
+ def ignore_tests
69
+ @ignore_tests ||= []
68
70
  end
69
71
 
70
72
  def self.ignore(test)
71
73
  ignore_tests.push(test) if test
72
74
  end
73
75
 
74
- def self.ignore_nothing
75
- @@ignore_tests = []
76
+ def ignore_nothing
77
+ @ignore_tests = []
76
78
  end
77
79
 
78
80
  def self.ignore?(event)
79
- ignore_tests.any?{|ignore_test| ignore_test.call(event)}
81
+ ignore_tests.any? { |ignore_test| ignore_test.call(event) }
80
82
  end
81
83
 
82
84
  # Loglines are emitted with this log level
@@ -102,7 +104,7 @@ module Lograge
102
104
  end
103
105
 
104
106
  def self.unsubscribe(component, subscriber)
105
- events = subscriber.public_methods(false).reject{ |method| method.to_s == 'call' }
107
+ events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' }
106
108
  events.each do |event|
107
109
  ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener|
108
110
  if listener.instance_variable_get('@delegate') == subscriber
@@ -113,26 +115,40 @@ module Lograge
113
115
  end
114
116
 
115
117
  def self.setup(app)
118
+ self.application = app
116
119
  app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache
117
- require 'lograge/rails_ext/rack/logger'
118
- Lograge.remove_existing_log_subscriptions
120
+
121
+ unless app.config.lograge.keep_original_rails_log
122
+ require 'lograge/rails_ext/rack/logger'
123
+ Lograge.remove_existing_log_subscriptions
124
+ end
125
+
126
+ Lograge.logger = app.config.lograge.logger
127
+
119
128
  Lograge::RequestLogSubscriber.attach_to :action_controller
120
- Lograge.custom_options = app.config.lograge.custom_options
121
- Lograge.before_format = app.config.lograge.before_format
122
- Lograge.log_level = app.config.lograge.log_level || :info
123
- self.support_deprecated_config(app) # TODO: Remove with version 1.0
124
- Lograge.formatter = app.config.lograge.formatter || Lograge::Formatters::KeyValue.new
125
- Lograge.ignore_actions(app.config.lograge.ignore_actions)
126
- Lograge.ignore(app.config.lograge.ignore_custom)
129
+ Lograge.custom_options = lograge_config.custom_options
130
+ Lograge.before_format = lograge_config.before_format
131
+ Lograge.log_level = lograge_config.log_level || :info
132
+ support_deprecated_config # TODO: Remove with version 1.0
133
+ Lograge.formatter = lograge_config.formatter || Lograge::Formatters::KeyValue.new
134
+ Lograge.ignore_actions(lograge_config.ignore_actions)
135
+ Lograge.ignore(lograge_config.ignore_custom)
127
136
  end
128
137
 
129
138
  # TODO: Remove with version 1.0
130
- def self.support_deprecated_config(app)
131
- if legacy_log_format = app.config.lograge.log_format
132
- ActiveSupport::Deprecation.warn 'config.lograge.log_format is deprecated. Use config.lograge.formatter instead.', caller
133
- legacy_log_format = :key_value if legacy_log_format == :lograge
134
- app.config.lograge.formatter = "Lograge::Formatters::#{legacy_log_format.to_s.classify}".constantize.new
135
- end
139
+
140
+ def support_deprecated_config
141
+ return unless lograge_config.log_format
142
+
143
+ legacy_log_format = lograge_config.log_format
144
+ warning = 'config.lograge.log_format is deprecated. Use config.lograge.formatter instead.'
145
+ ActiveSupport::Deprecation.warn(warning, caller)
146
+ legacy_log_format = :key_value if legacy_log_format == :lograge
147
+ lograge_config.formatter = "Lograge::Formatters::#{legacy_log_format.to_s.classify}".constantize.new
148
+ end
149
+
150
+ def lograge_config
151
+ application.config.lograge
136
152
  end
137
153
  end
138
154
 
metadata CHANGED
@@ -1,169 +1,129 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lograge
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
5
- prerelease:
4
+ version: 0.3.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Mathias Meyer
8
+ - Ben Lovell
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-11 00:00:00.000000000 Z
12
+ date: 2015-01-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
16
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
17
  requirements:
19
- - - ! '>='
18
+ - - ">="
20
19
  - !ruby/object:Gem::Version
21
20
  version: '0'
22
21
  type: :development
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
- - - ! '>='
25
+ - - ">="
28
26
  - !ruby/object:Gem::Version
29
27
  version: '0'
30
28
  - !ruby/object:Gem::Dependency
31
29
  name: guard-rspec
32
30
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
31
  requirements:
35
- - - ! '>='
32
+ - - ">="
36
33
  - !ruby/object:Gem::Version
37
34
  version: '0'
38
35
  type: :development
39
36
  prerelease: false
40
37
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
38
  requirements:
43
- - - ! '>='
39
+ - - ">="
44
40
  - !ruby/object:Gem::Version
45
41
  version: '0'
46
42
  - !ruby/object:Gem::Dependency
47
43
  name: activesupport
48
44
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
45
  requirements:
51
- - - ! '>='
46
+ - - ">="
52
47
  - !ruby/object:Gem::Version
53
48
  version: '3'
54
49
  type: :runtime
55
50
  prerelease: false
56
51
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
52
  requirements:
59
- - - ! '>='
53
+ - - ">="
60
54
  - !ruby/object:Gem::Version
61
55
  version: '3'
62
56
  - !ruby/object:Gem::Dependency
63
57
  name: actionpack
64
58
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
59
  requirements:
67
- - - ! '>='
60
+ - - ">="
68
61
  - !ruby/object:Gem::Version
69
62
  version: '3'
70
63
  type: :runtime
71
64
  prerelease: false
72
65
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
66
  requirements:
75
- - - ! '>='
67
+ - - ">="
76
68
  - !ruby/object:Gem::Version
77
69
  version: '3'
78
70
  - !ruby/object:Gem::Dependency
79
71
  name: railties
80
72
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
73
  requirements:
83
- - - ! '>='
74
+ - - ">="
84
75
  - !ruby/object:Gem::Version
85
76
  version: '3'
86
77
  type: :runtime
87
78
  prerelease: false
88
79
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
80
  requirements:
91
- - - ! '>='
81
+ - - ">="
92
82
  - !ruby/object:Gem::Version
93
83
  version: '3'
94
84
  description: Tame Rails' multi-line logging into a single line per request
95
85
  email:
96
86
  - meyer@paperplanes.de
87
+ - benjamin.lovell@gmail.com
97
88
  executables: []
98
89
  extensions: []
99
90
  extra_rdoc_files: []
100
91
  files:
101
- - .gitignore
102
- - .rspec
103
- - .travis.yml
104
- - CHANGES.md
105
- - Gemfile
106
- - Guardfile
107
- - README.md
108
- - Rakefile
109
92
  - lib/lograge.rb
110
93
  - lib/lograge/formatters/cee.rb
111
94
  - lib/lograge/formatters/graylog2.rb
112
95
  - lib/lograge/formatters/json.rb
113
96
  - lib/lograge/formatters/key_value.rb
114
97
  - lib/lograge/formatters/l2met.rb
98
+ - lib/lograge/formatters/lines.rb
115
99
  - lib/lograge/formatters/logstash.rb
116
100
  - lib/lograge/formatters/raw.rb
117
101
  - lib/lograge/log_subscriber.rb
118
102
  - lib/lograge/rails_ext/rack/logger.rb
119
103
  - lib/lograge/railtie.rb
120
104
  - lib/lograge/version.rb
121
- - lograge.gemspec
122
- - spec/formatters/cee_spec.rb
123
- - spec/formatters/graylog2_spec.rb
124
- - spec/formatters/json_spec.rb
125
- - spec/formatters/key_value_spec.rb
126
- - spec/formatters/l2met_spec.rb
127
- - spec/formatters/logstash_spec.rb
128
- - spec/formatters/raw_spec.rb
129
- - spec/lograge_logsubscriber_spec.rb
130
- - spec/lograge_spec.rb
131
- - spec/spec_helper.rb
132
- - spec/support/examples.rb
133
105
  homepage: https://github.com/roidrage/lograge
134
- licenses: []
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
135
109
  post_install_message:
136
110
  rdoc_options: []
137
111
  require_paths:
138
112
  - lib
139
113
  required_ruby_version: !ruby/object:Gem::Requirement
140
- none: false
141
114
  requirements:
142
- - - ! '>='
115
+ - - ">="
143
116
  - !ruby/object:Gem::Version
144
117
  version: '0'
145
118
  required_rubygems_version: !ruby/object:Gem::Requirement
146
- none: false
147
119
  requirements:
148
- - - ! '>='
120
+ - - ">="
149
121
  - !ruby/object:Gem::Version
150
122
  version: '0'
151
123
  requirements: []
152
- rubyforge_project: lograge
153
- rubygems_version: 1.8.23
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.5
154
126
  signing_key:
155
- specification_version: 3
127
+ specification_version: 4
156
128
  summary: Tame Rails' multi-line logging into a single line per request
157
- test_files:
158
- - spec/formatters/cee_spec.rb
159
- - spec/formatters/graylog2_spec.rb
160
- - spec/formatters/json_spec.rb
161
- - spec/formatters/key_value_spec.rb
162
- - spec/formatters/l2met_spec.rb
163
- - spec/formatters/logstash_spec.rb
164
- - spec/formatters/raw_spec.rb
165
- - spec/lograge_logsubscriber_spec.rb
166
- - spec/lograge_spec.rb
167
- - spec/spec_helper.rb
168
- - spec/support/examples.rb
169
- has_rdoc:
129
+ test_files: []