log4r-exceptionable 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ 0.7.0 (06/21/2012)
2
+ ------------------
3
+
4
+ allow logging exceptions with custom log level, default to fatal <3db1839>
5
+
1
6
  0.6.0 (06/18/2012)
2
7
  ------------------
3
8
 
data/README.md CHANGED
@@ -33,6 +33,11 @@ Add to some initializer code:
33
33
  #
34
34
  # config.use_source_logger = false
35
35
 
36
+ # Log at the given log level
37
+ # optional - defaults to :fatal
38
+ #
39
+ # config.log_level = :error
40
+
36
41
  # A whitelist of the context keys to include when logging.
37
42
  # If this is set, _only_these keys will show up.
38
43
  # optional - defaults to nil, so all keys get included
@@ -9,6 +9,8 @@ module Log4rExceptionable
9
9
  attr_accessor :rack_failure_logger, :resque_failure_logger
10
10
  # Allows one to force use of default loggers by setting to false
11
11
  attr_accessor :use_source_logger
12
+ # The level to log exceptions
13
+ attr_accessor :log_level
12
14
  # whitelist of context keys (e.g. keys in rack env) to include in log4r context when logging
13
15
  attr_accessor :context_inclusions
14
16
  # blacklist of context keys (e.g. keys in rack env) to exclude in log4r context when logging
@@ -17,6 +19,7 @@ module Log4rExceptionable
17
19
 
18
20
  # default values
19
21
  self.use_source_logger = true
22
+ self.log_level = :fatal
20
23
 
21
24
  def self.configure
22
25
  yield self
@@ -35,7 +38,9 @@ module Log4rExceptionable
35
38
 
36
39
  self.context_inclusions = Set.new(self.context_inclusions) if self.context_inclusions
37
40
  self.context_exclusions = Set.new(self.context_exclusions) if self.context_exclusions
38
-
41
+
42
+ raise "Invalid log level: #{self.log_level}" unless Log4r::LNAMES.include?(self.log_level.to_s.upcase)
43
+ self.log_level = self.log_level.to_sym
39
44
  end
40
45
 
41
46
  def self.set_logger(accessor)
@@ -71,7 +71,7 @@ module Log4rExceptionable
71
71
 
72
72
  error_logger ||= Log4rExceptionable::Configuration.rack_failure_logger
73
73
 
74
- error_logger.error(exception)
74
+ error_logger.send(Log4rExceptionable::Configuration.log_level, exception)
75
75
 
76
76
  end
77
77
 
@@ -36,7 +36,7 @@ module Log4rExceptionable
36
36
 
37
37
  error_logger ||= Log4rExceptionable::Configuration.resque_failure_logger
38
38
 
39
- error_logger.error(exception)
39
+ error_logger.send(Log4rExceptionable::Configuration.log_level, exception)
40
40
  end
41
41
 
42
42
  end
@@ -1,3 +1,3 @@
1
1
  module Log4rExceptionable
2
- VERSION = "0.6.0"
2
+ VERSION = "0.7.0"
3
3
  end
@@ -60,6 +60,31 @@ describe Log4rExceptionable::Configuration do
60
60
  Log4rExceptionable::Configuration.rack_failure_logger.should == Log4r::Logger['otherlogger']
61
61
  end
62
62
 
63
+ it "should raise if invalid log_level" do
64
+ lambda {
65
+ Log4rExceptionable::Configuration.configure do |config|
66
+ config.rack_failure_logger = "mylogger"
67
+ config.log_level = nil
68
+ end
69
+ }.should raise_error("Invalid log level: ")
70
+
71
+ lambda {
72
+ Log4rExceptionable::Configuration.configure do |config|
73
+ config.rack_failure_logger = "mylogger"
74
+ config.log_level = :foobar
75
+ end
76
+ }.should raise_error("Invalid log level: foobar")
77
+ end
78
+
79
+ it "should allow setting valid log_level" do
80
+ lambda {
81
+ Log4rExceptionable::Configuration.configure do |config|
82
+ config.rack_failure_logger = "mylogger"
83
+ config.log_level = :debug
84
+ end
85
+ }.should_not raise_error
86
+ end
87
+
63
88
  end
64
89
 
65
90
  end
@@ -64,16 +64,17 @@ describe Log4rExceptionable::RackFailureHandler do
64
64
  config.use_source_logger = true
65
65
  config.context_inclusions = nil
66
66
  config.context_exclusions = nil
67
+ config.log_level = :fatal
67
68
  end
68
69
  end
69
70
 
70
71
  it "doesn't log a failure with normal usage" do
71
- Log4r::Logger['racklogger'].should_not_receive(:error)
72
+ Log4r::Logger['racklogger'].should_not_receive(:fatal)
72
73
  get "/"
73
74
  end
74
75
 
75
76
  it "triggers failure handler" do
76
- Log4r::Logger['racklogger'].should_receive(:error) do |msg|
77
+ Log4r::Logger['racklogger'].should_receive(:fatal) do |msg|
77
78
  msg.should be_instance_of RuntimeError
78
79
  msg.message.should == "I failed"
79
80
  msg.backtrace.first.should =~ /rack_failure_handler_spec.rb/
@@ -87,7 +88,7 @@ describe Log4rExceptionable::RackFailureHandler do
87
88
 
88
89
  it "uses default logger if controller logger is nil" do
89
90
 
90
- Log4r::Logger['racklogger'].should_receive(:error) do |msg|
91
+ Log4r::Logger['racklogger'].should_receive(:fatal) do |msg|
91
92
  msg.should be_instance_of RuntimeError
92
93
  msg.message.should == "I failed"
93
94
  end
@@ -99,7 +100,7 @@ describe Log4rExceptionable::RackFailureHandler do
99
100
 
100
101
  it "uses default logger if controller logger is not log4r" do
101
102
 
102
- Log4r::Logger['racklogger'].should_receive(:error) do |msg|
103
+ Log4r::Logger['racklogger'].should_receive(:fatal) do |msg|
103
104
  msg.should be_instance_of RuntimeError
104
105
  msg.message.should == "I failed"
105
106
  end
@@ -111,8 +112,8 @@ describe Log4rExceptionable::RackFailureHandler do
111
112
 
112
113
  it "uses controller logger if set" do
113
114
  Log4r::Logger.new('ControllerLogger')
114
- Log4r::Logger['racklogger'].should_not_receive(:error)
115
- Log4r::Logger['ControllerLogger'].should_receive(:error) do |msg|
115
+ Log4r::Logger['racklogger'].should_not_receive(:fatal)
116
+ Log4r::Logger['ControllerLogger'].should_receive(:fatal) do |msg|
116
117
  msg.should be_instance_of RuntimeError
117
118
  msg.message.should == "I failed"
118
119
  end
@@ -125,8 +126,8 @@ describe Log4rExceptionable::RackFailureHandler do
125
126
  it "uses default logger if source logger disabled" do
126
127
  Log4rExceptionable::Configuration.use_source_logger = false
127
128
  Log4r::Logger.new('ControllerLogger')
128
- Log4r::Logger['ControllerLogger'].should_not_receive(:error)
129
- Log4r::Logger['racklogger'].should_receive(:error) do |msg|
129
+ Log4r::Logger['ControllerLogger'].should_not_receive(:fatal)
130
+ Log4r::Logger['racklogger'].should_receive(:fatal) do |msg|
130
131
  msg.should be_instance_of RuntimeError
131
132
  msg.message.should == "I failed"
132
133
  end
@@ -138,7 +139,7 @@ describe Log4rExceptionable::RackFailureHandler do
138
139
 
139
140
  it "adds controller names if set" do
140
141
  Log4r::Logger.new('ControllerLogger')
141
- Log4r::Logger['ControllerLogger'].should_receive(:error) do |msg|
142
+ Log4r::Logger['ControllerLogger'].should_receive(:fatal) do |msg|
142
143
  msg.should be_instance_of RuntimeError
143
144
  msg.message.should == "I failed"
144
145
  Log4r::MDC.get('rack_controller_name').should == 'foo'
@@ -152,7 +153,7 @@ describe Log4rExceptionable::RackFailureHandler do
152
153
 
153
154
  it "uses default logger if rack logger is nil" do
154
155
 
155
- Log4r::Logger['racklogger'].should_receive(:error) do |msg|
156
+ Log4r::Logger['racklogger'].should_receive(:fatal) do |msg|
156
157
  msg.should be_instance_of RuntimeError
157
158
  msg.message.should == "I failed"
158
159
  end
@@ -163,7 +164,7 @@ describe Log4rExceptionable::RackFailureHandler do
163
164
  end
164
165
 
165
166
  it "uses default logger if rack logger is not log4r" do
166
- Log4r::Logger['racklogger'].should_receive(:error) do |msg|
167
+ Log4r::Logger['racklogger'].should_receive(:fatal) do |msg|
167
168
  msg.should be_instance_of RuntimeError
168
169
  msg.message.should == "I failed"
169
170
  end
@@ -175,8 +176,8 @@ describe Log4rExceptionable::RackFailureHandler do
175
176
 
176
177
  it "uses rack logger if set" do
177
178
  Log4r::Logger.new('RackLogger')
178
- Log4r::Logger['racklogger'].should_not_receive(:error)
179
- Log4r::Logger['RackLogger'].should_receive(:error) do |msg|
179
+ Log4r::Logger['racklogger'].should_not_receive(:fatal)
180
+ Log4r::Logger['RackLogger'].should_receive(:fatal) do |msg|
180
181
  msg.should be_instance_of RuntimeError
181
182
  msg.message.should == "I failed"
182
183
  end
@@ -189,7 +190,7 @@ describe Log4rExceptionable::RackFailureHandler do
189
190
  it "only includes inclusions if set" do
190
191
  Log4rExceptionable::Configuration.context_inclusions = ['rack_env_rack.version']
191
192
 
192
- Log4r::Logger['racklogger'].should_receive(:error) do |msg|
193
+ Log4r::Logger['racklogger'].should_receive(:fatal) do |msg|
193
194
  msg.should be_instance_of RuntimeError
194
195
  msg.message.should == "I failed"
195
196
  Log4r::MDC.get_context.keys.should == ['rack_env_rack.version']
@@ -204,7 +205,7 @@ describe Log4rExceptionable::RackFailureHandler do
204
205
  it "excludes exclusions if set" do
205
206
  Log4rExceptionable::Configuration.context_exclusions = ['rack_env_rack.version']
206
207
 
207
- Log4r::Logger['racklogger'].should_receive(:error) do |msg|
208
+ Log4r::Logger['racklogger'].should_receive(:fatal) do |msg|
208
209
  msg.should be_instance_of RuntimeError
209
210
  msg.message.should == "I failed"
210
211
  Log4r::MDC.get_context.keys.should_not include 'rack_env_rack.version'
@@ -216,6 +217,19 @@ describe Log4rExceptionable::RackFailureHandler do
216
217
 
217
218
  end
218
219
 
220
+ it "logs with given log_level" do
221
+ Log4rExceptionable::Configuration.log_level = :info
222
+
223
+ Log4r::Logger['racklogger'].should_receive(:info) do |msg|
224
+ msg.should be_instance_of RuntimeError
225
+ msg.message.should == "I failed"
226
+ end
227
+
228
+ lambda {
229
+ get "/other_rack_logger"
230
+ }.should raise_error("I failed")
231
+ end
232
+
219
233
  end
220
234
 
221
235
  end
@@ -47,6 +47,7 @@ describe Log4rExceptionable::ResqueFailureHandler do
47
47
  config.use_source_logger = true
48
48
  config.context_inclusions = nil
49
49
  config.context_exclusions = nil
50
+ config.log_level = :fatal
50
51
  end
51
52
 
52
53
  Resque::Failure.backend = Log4rExceptionable::ResqueFailureHandler
@@ -54,7 +55,7 @@ describe Log4rExceptionable::ResqueFailureHandler do
54
55
 
55
56
  it "triggers failure handler" do
56
57
 
57
- Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
58
+ Log4r::Logger['resquelogger'].should_receive(:fatal) do |msg|
58
59
  msg.should be_instance_of RuntimeError
59
60
  msg.message.should == "I failed"
60
61
  msg.backtrace.first.should =~ /resque_failure_handler_spec.rb/
@@ -69,7 +70,7 @@ describe Log4rExceptionable::ResqueFailureHandler do
69
70
 
70
71
  it "uses default logger if job logger is nil" do
71
72
 
72
- Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
73
+ Log4r::Logger['resquelogger'].should_receive(:fatal) do |msg|
73
74
  msg.should be_instance_of RuntimeError
74
75
  msg.message.should == "I failed"
75
76
  Log4r::MDC.get('resque_class').should == SomeJobWithNilLogger
@@ -80,7 +81,7 @@ describe Log4rExceptionable::ResqueFailureHandler do
80
81
 
81
82
  it "uses default logger if job logger is not log4r" do
82
83
 
83
- Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
84
+ Log4r::Logger['resquelogger'].should_receive(:fatal) do |msg|
84
85
  msg.should be_instance_of RuntimeError
85
86
  msg.message.should == "I failed"
86
87
  Log4r::MDC.get('resque_class').should == SomeJobWithOtherLogger
@@ -91,8 +92,8 @@ describe Log4rExceptionable::ResqueFailureHandler do
91
92
 
92
93
  it "uses job logger if set" do
93
94
  Log4r::Logger.new('SomeJobWithLogger')
94
- Log4r::Logger['resquelogger'].should_not_receive(:error)
95
- Log4r::Logger['SomeJobWithLogger'].should_receive(:error) do |msg|
95
+ Log4r::Logger['resquelogger'].should_not_receive(:fatal)
96
+ Log4r::Logger['SomeJobWithLogger'].should_receive(:fatal) do |msg|
96
97
  msg.should be_instance_of RuntimeError
97
98
  msg.message.should == "I failed"
98
99
  Log4r::MDC.get('resque_class').should == SomeJobWithLogger
@@ -105,8 +106,8 @@ describe Log4rExceptionable::ResqueFailureHandler do
105
106
  Log4rExceptionable::Configuration.use_source_logger = false
106
107
 
107
108
  Log4r::Logger.new('SomeJobWithLogger')
108
- Log4r::Logger['SomeJobWithLogger'].should_not_receive(:error)
109
- Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
109
+ Log4r::Logger['SomeJobWithLogger'].should_not_receive(:fatal)
110
+ Log4r::Logger['resquelogger'].should_receive(:fatal) do |msg|
110
111
  msg.should be_instance_of RuntimeError
111
112
  msg.message.should == "I failed"
112
113
  Log4r::MDC.get('resque_class').should == SomeJobWithLogger
@@ -118,7 +119,7 @@ describe Log4rExceptionable::ResqueFailureHandler do
118
119
  it "only includes inclusions if set" do
119
120
  Log4rExceptionable::Configuration.context_inclusions = ['resque_queue']
120
121
 
121
- Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
122
+ Log4r::Logger['resquelogger'].should_receive(:fatal) do |msg|
122
123
  msg.should be_instance_of RuntimeError
123
124
  msg.message.should == "I failed"
124
125
  Log4r::MDC.get_context.keys.should == ['resque_queue']
@@ -130,7 +131,7 @@ describe Log4rExceptionable::ResqueFailureHandler do
130
131
  it "excludes exclusions if set" do
131
132
  Log4rExceptionable::Configuration.context_exclusions = ['resque_queue']
132
133
 
133
- Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
134
+ Log4r::Logger['resquelogger'].should_receive(:fatal) do |msg|
134
135
  msg.should be_instance_of RuntimeError
135
136
  msg.message.should == "I failed"
136
137
  Log4r::MDC.get_context.keys.should_not include 'resque_queue'
@@ -139,6 +140,18 @@ describe Log4rExceptionable::ResqueFailureHandler do
139
140
  run_resque_job(SomeJob, 'foo', :queue => :somequeue, :inline => true)
140
141
  end
141
142
 
143
+ it "logs with given log_level" do
144
+ Log4rExceptionable::Configuration.log_level = :info
145
+
146
+ Log4r::Logger['resquelogger'].should_receive(:info) do |msg|
147
+ msg.should be_instance_of RuntimeError
148
+ msg.message.should == "I failed"
149
+ end
150
+
151
+ run_resque_job(SomeJob, 'foo', :queue => :somequeue, :inline => true)
152
+ end
153
+
154
+
142
155
  end
143
156
 
144
157
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: log4r-exceptionable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.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-06-18 00:00:00.000000000 Z
12
+ date: 2012-06-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake