lograge 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -7,6 +7,10 @@ default logging output. Rails' default approach to log everything is great
7
7
  during development, it's terrible when running it in production. It pretty much
8
8
  renders Rails logs useless to me.
9
9
 
10
+ Lograge is a work in progress. I appreciate constructive feedback and criticism.
11
+ My main goal is to improve Rails' logging and to show people that they don't
12
+ need to stick with its defaults anymore if they don't want to.
13
+
10
14
  Instead of trying solving the problem of having multiple lines per request by
11
15
  switching Rails' logger for something that outputs syslog lines or adds a
12
16
  request token, Lograge replaces Rails' request logging entirely, reducing the
@@ -51,7 +55,23 @@ Enable it for the relevant environments, e.g. production:
51
55
  ```
52
56
  # config/environments/production.rb
53
57
  MyApp::Application.configure do
54
- config.lograge.enabled = true
58
+ config.lograge.enabled = true
59
+ end
60
+ ```
61
+
62
+ You can also add a hook for own custom data
63
+
64
+ ```
65
+ # config/environments/staging.rb
66
+ MyApp::Application.configure do
67
+ config.lograge.enabled = true
68
+
69
+ # custom_options can be a lambda or hash
70
+ # if it's a lambda then it must return a hash
71
+ config.lograge.custom_options = lambda do |event|
72
+ # capture some specific timing values you are interested in
73
+ {:name => "value", :name => "%2f" % float}
74
+ end
55
75
  end
56
76
  ```
57
77
 
@@ -79,16 +99,24 @@ While the LogSubscribers encapsulate most logging pretty nicely, there are still
79
99
  two lines that show up no matter what. The first line that's output for every
80
100
  Rails request, you know, this one:
81
101
 
102
+ ```
103
+ Started GET "/" for 127.0.0.1 at 2012-03-12 17:10:10 +0100
104
+ ```
105
+
82
106
  And the verbose output coming from rack-cache:
83
107
 
108
+ ```
109
+ cache: [GET /] miss
110
+ ```
111
+
84
112
  Both are independent of the LogSubscribers, and both need to be shut up using
85
113
  different means.
86
114
 
87
- For the first one, the starting line of every Rails request log, Lograge removes
88
- the `Rails::Rack::Logger` middleware from the stack. This may look like a drastic
89
- means, but all the middleware does is log that useless line, log exceptions, and
90
- create a request transaction id (Rails 3.2). A future version may replace with
91
- its own middleware, that simply removes the log line.
115
+ For the first one, the starting line of every Rails request log, Lograge
116
+ replaces code in `Rails::Rack::Logger` to remove that particular log line. It's
117
+ not great, but it's just another unnecessary output and would still clutter the
118
+ log files. Maybe a future version of Rails will make this log line an event as
119
+ well.
92
120
 
93
121
  To remove rack-cache's output (which is only enabled if caching in Rails is
94
122
  enabled), Lograge disables verbosity for rack-cache, which is unfortunately
@@ -98,6 +126,9 @@ There, a single line per request. Beautiful.
98
126
 
99
127
  **What it doesn't do**
100
128
 
129
+ Lograge is opinionated, very opinionated. If the stuff below doesn't suit your
130
+ needs, it may not be for you.
131
+
101
132
  Lograge removes ActionView logging, which also includes rendering times for
102
133
  partials. If you're into those, Lograge is probably not for you. In my honest
103
134
  opinion, those rendering times don't belong in the log file, they should be
@@ -106,6 +137,15 @@ service that allows graphing rendering percentiles. I assume this for everything
106
137
  that represents a moving target. That kind of data is better off being
107
138
  visualized in graphs than dumped (and ignored) in a log file.
108
139
 
140
+ Lograge doesn't yet log the request parameters. This is something I'm actively
141
+ contemplating, mainly because I want to find a good way to include them, a way
142
+ that fits in with the general spirit of the log output generated by Lograge.
143
+
144
+ **Changes**
145
+
146
+ * Add `custom_options` to allow adding custom key-value pairs at runtime (Adam
147
+ Cooper, https://github.com/adamcooper)
148
+
109
149
  **License**
110
150
 
111
151
  MIT. Code extracted from [Travis CI](http://travis-ci.org).
data/lib/lograge.rb CHANGED
@@ -5,15 +5,32 @@ require 'active_support/core_ext/string/inflections'
5
5
  require 'active_support/ordered_options'
6
6
 
7
7
  module Lograge
8
- mattr_accessor :logger
8
+ mattr_accessor :logger
9
+
10
+ # Custom options that will be appended to log line
11
+ #
12
+ # Currently supported formats are:
13
+ # - Hash
14
+ # - Any object that responds to call and returns a hash
15
+ #
16
+ mattr_writer :custom_options
17
+ self.custom_options = nil
18
+
19
+ def self.custom_options(event)
20
+ if @@custom_options.respond_to?(:call)
21
+ @@custom_options.call(event)
22
+ else
23
+ @@custom_options
24
+ end
25
+ end
9
26
 
10
27
  def self.remove_existing_log_subscriptions
11
- %w(redirect_to process_action start_processing send_data write_fragment exist_fragment? send_file).each do |event|
28
+ %w(redirect_to process_action start_processing send_data send_file write_fragment read_fragment exist_fragment? expire_fragment expire_page write_page).each do |event|
12
29
  unsubscribe_from_event(:action_controller, event)
13
30
  end
14
31
 
15
32
  %w{render_template render_partial render_collection}.each do |event|
16
- unsubscribe_from_event(:action_view, event)
33
+ unsubscribe_from_event(:action_view, event)
17
34
  end
18
35
  end
19
36
 
@@ -27,10 +44,11 @@ module Lograge
27
44
  end
28
45
 
29
46
  def self.setup(app)
30
- app.config.action_dispatch.rack_cache[:verbose] = false
47
+ app.config.action_dispatch.rack_cache[:verbose] = false if app.config.action_dispatch.rack_cache
31
48
  require 'lograge/rails_ext/rack/logger'
32
49
  Lograge.remove_existing_log_subscriptions
33
50
  Lograge::RequestLogSubscriber.attach_to :action_controller
51
+ Lograge.custom_options = app.config.lograge.custom_options
34
52
  end
35
53
  end
36
54
 
@@ -9,6 +9,7 @@ module Lograge
9
9
  message << extract_status(payload)
10
10
  message << runtimes(event)
11
11
  message << location(event)
12
+ message << custom_options(event)
12
13
  logger.info(message)
13
14
  end
14
15
 
@@ -27,6 +28,14 @@ module Lograge
27
28
  end
28
29
  end
29
30
 
31
+ def custom_options(event)
32
+ message = ""
33
+ (Lograge.custom_options(event) || {}).each do |name, value|
34
+ message << " #{name}=#{value}"
35
+ end
36
+ message
37
+ end
38
+
30
39
  def runtimes(event)
31
40
  message = ""
32
41
  {:duration => event.duration,
@@ -40,10 +49,10 @@ module Lograge
40
49
  def location(event)
41
50
  if location = Thread.current[:lograge_location]
42
51
  Thread.current[:lograge_location] = nil
43
- "location=#{location}"
52
+ " location=#{location}"
44
53
  else
45
54
  ""
46
55
  end
47
- end
56
+ end
48
57
  end
49
58
  end
@@ -1,3 +1,3 @@
1
1
  module Lograge
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lograge.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = Lograge::VERSION
8
8
  s.authors = ["Mathias Meyer"]
9
9
  s.email = ["meyer@paperplanes.de"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/mattmatt/lograge"
11
11
  s.summary = %q{Tame Rails' multi-line logging into a single line per request}
12
12
  s.description = %q{Tame Rails' multi-line logging into a single line per request}
13
13
 
@@ -1,4 +1,5 @@
1
1
  require 'spec_helper'
2
+ require 'lograge'
2
3
  require 'lograge/log_subscriber'
3
4
  require 'active_support/notifications'
4
5
  require 'active_support/core_ext/string'
@@ -14,7 +15,7 @@ describe Lograge::RequestLogSubscriber do
14
15
  logger
15
16
  }
16
17
  before do
17
- Lograge::RequestLogSubscriber.logger = logger
18
+ Lograge::RequestLogSubscriber.logger = logger
18
19
  end
19
20
 
20
21
  let(:subscriber) {Lograge::RequestLogSubscriber.new}
@@ -44,7 +45,7 @@ describe Lograge::RequestLogSubscriber do
44
45
  subscriber.process_action(event)
45
46
  log_output.string.starts_with?('GET').should == true
46
47
  end
47
-
48
+
48
49
  it "should include the status code" do
49
50
  subscriber.process_action(event)
50
51
  log_output.string.should include('status=200')
@@ -85,7 +86,7 @@ describe Lograge::RequestLogSubscriber do
85
86
 
86
87
  it "should add the location to the log line" do
87
88
  subscriber.process_action(event)
88
- log_output.string.should =~ %r{location=http://www.example.com}
89
+ log_output.string.should =~ %r{ location=http://www.example.com}
89
90
  end
90
91
 
91
92
  it "should remove the thread local variable" do
@@ -93,13 +94,31 @@ describe Lograge::RequestLogSubscriber do
93
94
  Thread.current[:lograge_location].should == nil
94
95
  end
95
96
  end
96
-
97
+
97
98
  it "should not include a location by default" do
98
99
  subscriber.process_action(event)
99
100
  log_output.string.should_not =~ /location=/
100
101
  end
101
102
  end
102
103
 
104
+ describe "with custom_options configured" do
105
+ it "should combine the hash properly for the output" do
106
+ Lograge.custom_options = {:data => "value"}
107
+ subscriber.process_action(event)
108
+ log_output.string.should =~ / data=value/
109
+ end
110
+ it "should combine the output of a lambda properly" do
111
+ Lograge.custom_options = lambda {|event| {:data => "value"}}
112
+ subscriber.process_action(event)
113
+ log_output.string.should =~ / data=value/
114
+ end
115
+ it "should work if the method returns nil" do
116
+ Lograge.custom_options = lambda {|event| nil}
117
+ subscriber.process_action(event)
118
+ log_output.string.should be_present
119
+ end
120
+ end
121
+
103
122
  describe "when processing a redirect" do
104
123
  it "should store the location in a thread local variable" do
105
124
  subscriber.redirect_to(redirect)
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.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-11 00:00:00.000000000 Z
12
+ date: 2012-04-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70153872860480 !ruby/object:Gem::Requirement
16
+ requirement: &70345743126700 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70153872860480
24
+ version_requirements: *70345743126700
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: guard-rspec
27
- requirement: &70153872859680 !ruby/object:Gem::Requirement
27
+ requirement: &70345743136340 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70153872859680
35
+ version_requirements: *70345743136340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activesupport
38
- requirement: &70153872858340 !ruby/object:Gem::Requirement
38
+ requirement: &70345743177800 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70153872858340
46
+ version_requirements: *70345743177800
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: actionpack
49
- requirement: &70153872856860 !ruby/object:Gem::Requirement
49
+ requirement: &70345743191500 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70153872856860
57
+ version_requirements: *70345743191500
58
58
  description: Tame Rails' multi-line logging into a single line per request
59
59
  email:
60
60
  - meyer@paperplanes.de
@@ -78,7 +78,7 @@ files:
78
78
  - spec/lograge_logsubscriber_spec.rb
79
79
  - spec/lograge_spec.rb
80
80
  - spec/spec_helper.rb
81
- homepage: ''
81
+ homepage: https://github.com/mattmatt/lograge
82
82
  licenses: []
83
83
  post_install_message:
84
84
  rdoc_options: []