log4r-exceptionable 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ 0.5.3 (06/15/2012)
2
+ ------------------
3
+
4
+ add in controller/action names from rails as custom attributes <a83c1f5>
5
+ delegate exception formatting to log4r by passing exception object to logger instead of creating message manually <d9526d3>
6
+ store exception line as an int not string <9de126a>
7
+
1
8
  0.5.2 (06/15/2012)
2
9
  ------------------
3
10
 
@@ -40,24 +40,13 @@ module Log4rExceptionable
40
40
  original_mdc = mdc.get_context
41
41
 
42
42
  begin
43
- message = "#{exception.class}: #{exception.message}"
44
-
45
- mdc.put('rack_exception', exception.class.name)
46
- trace = Array(exception.backtrace)
47
- if trace.size > 0
48
- message << "\n"
49
- message << trace.join("\n")
50
43
 
51
- file, line = trace[0].split(":")
52
- mdc.put('rack_exception_file', file)
53
- mdc.put('rack_exception_line', line)
54
- end
55
-
56
44
  if env and env.size > 0
57
45
  env.each do |k, v|
58
46
  begin
59
47
  mdc.put("rack_env_#{k}", v.inspect)
60
48
  rescue
49
+ puts "Log4r Exceptionable could not extract a rack env item: " + e.message
61
50
  end
62
51
  end
63
52
  end
@@ -65,13 +54,19 @@ module Log4rExceptionable
65
54
  controller = env['action_controller.instance']
66
55
  if controller && controller.respond_to?(:logger) && controller.logger.instance_of?(Log4r::Logger)
67
56
  error_logger = controller.logger
57
+ begin
58
+ mdc.put("rack_controller_name", controller.controller_name.to_s)
59
+ mdc.put("rack_action_name", controller.action_name.to_s)
60
+ rescue => e
61
+ puts "Log4r Exceptionable could not extract controller names: " + e.message
62
+ end
68
63
  elsif env['rack.logger'] && env['rack.logger'].instance_of?(Log4r::Logger)
69
64
  error_logger = env['rack.logger']
70
65
  else
71
66
  error_logger = Log4rExceptionable::Configuration.rack_failure_logger
72
67
  end
73
68
 
74
- error_logger.error(message)
69
+ error_logger.error(exception)
75
70
  ensure
76
71
  # Since this is somewhat of a global map, clean the keys
77
72
  # we put in so other log messages don't see them
@@ -12,23 +12,18 @@ module Log4rExceptionable
12
12
  original_mdc = mdc.get_context
13
13
 
14
14
  begin
15
- message = "#{exception.class}: #{exception.message}"
16
-
17
- mdc.put('resque_exception', exception.class.name)
18
- trace = Array(exception.backtrace)
19
- if trace.size > 0
20
- message << "\n"
21
- message << trace.join("\n")
22
-
23
- file, line = trace[0].split(":")
24
- mdc.put('resque_exception_file', file)
25
- mdc.put('resque_exception_line', line)
26
- end
27
-
15
+
16
+ data = payload.clone
28
17
  mdc.put("resque_worker", worker.to_s)
29
18
  mdc.put("resque_queue", queue.to_s)
30
- mdc.put("resque_class", payload['class'].to_s)
31
- mdc.put("resque_args", payload['args'].inspect.to_s)
19
+ mdc.put("resque_class", data.delete('class').to_s)
20
+ mdc.put("resque_args", data.delete('args').inspect.to_s)
21
+
22
+ # add in any extra payload data, in case resque plugins have
23
+ # added to it (e.g. resque-lifecycle)
24
+ data.each do |k, v|
25
+ mdc.put("resque_payload_#{k}", v.inspect.to_s)
26
+ end
32
27
 
33
28
  payload_class = Resque.constantize(payload['class']) rescue nil
34
29
  if payload_class && payload_class.respond_to?(:logger) && payload_class.logger.instance_of?(Log4r::Logger)
@@ -37,7 +32,7 @@ module Log4rExceptionable
37
32
  error_logger = Log4rExceptionable::Configuration.resque_failure_logger
38
33
  end
39
34
 
40
- error_logger.error(message)
35
+ error_logger.error(exception)
41
36
  ensure
42
37
  # Since this is somewhat of a global map, clean the keys
43
38
  # we put in so other log messages don't see them
@@ -1,3 +1,3 @@
1
1
  module Log4rExceptionable
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -10,6 +10,12 @@ describe Log4rExceptionable::RackFailureHandler do
10
10
  def logger
11
11
  self.class.logger
12
12
  end
13
+ def controller_name
14
+ "foo"
15
+ end
16
+ def action_name
17
+ "bar"
18
+ end
13
19
  end
14
20
 
15
21
  class TestApp
@@ -65,10 +71,9 @@ describe Log4rExceptionable::RackFailureHandler do
65
71
 
66
72
  it "triggers failure handler" do
67
73
  Log4r::Logger['racklogger'].should_receive(:error) do |msg|
68
- msg.should =~ /^RuntimeError: I failed/
69
- msg.should =~ /rack_failure_handler_spec.rb/
70
- Log4r::MDC.get('rack_exception_line').should =~ /\d+/
71
- Log4r::MDC.get('rack_exception_file').should =~ /rack_failure_handler_spec.rb/
74
+ msg.should be_instance_of RuntimeError
75
+ msg.message.should == "I failed"
76
+ msg.backtrace.first.should =~ /rack_failure_handler_spec.rb/
72
77
  Log4r::MDC.get('rack_env_PATH_INFO').should == '"/error"'
73
78
  end
74
79
 
@@ -80,7 +85,8 @@ describe Log4rExceptionable::RackFailureHandler do
80
85
  it "uses default logger if controller logger is nil" do
81
86
 
82
87
  Log4r::Logger['racklogger'].should_receive(:error) do |msg|
83
- msg.should =~ /^RuntimeError: I failed/
88
+ msg.should be_instance_of RuntimeError
89
+ msg.message.should == "I failed"
84
90
  end
85
91
 
86
92
  lambda {
@@ -91,7 +97,8 @@ describe Log4rExceptionable::RackFailureHandler do
91
97
  it "uses default logger if controller logger is not log4r" do
92
98
 
93
99
  Log4r::Logger['racklogger'].should_receive(:error) do |msg|
94
- msg.should =~ /^RuntimeError: I failed/
100
+ msg.should be_instance_of RuntimeError
101
+ msg.message.should == "I failed"
95
102
  end
96
103
 
97
104
  lambda {
@@ -103,7 +110,22 @@ describe Log4rExceptionable::RackFailureHandler do
103
110
  Log4r::Logger.new('ControllerLogger')
104
111
  Log4r::Logger['racklogger'].should_not_receive(:error)
105
112
  Log4r::Logger['ControllerLogger'].should_receive(:error) do |msg|
106
- msg.should =~ /^RuntimeError: I failed/
113
+ msg.should be_instance_of RuntimeError
114
+ msg.message.should == "I failed"
115
+ end
116
+
117
+ lambda {
118
+ get "/controller_logger"
119
+ }.should raise_error("I failed")
120
+ end
121
+
122
+ it "adds controller names if set" do
123
+ Log4r::Logger.new('ControllerLogger')
124
+ Log4r::Logger['ControllerLogger'].should_receive(:error) do |msg|
125
+ msg.should be_instance_of RuntimeError
126
+ msg.message.should == "I failed"
127
+ Log4r::MDC.get('rack_controller_name').should == 'foo'
128
+ Log4r::MDC.get('rack_action_name').should == 'bar'
107
129
  end
108
130
 
109
131
  lambda {
@@ -114,7 +136,8 @@ describe Log4rExceptionable::RackFailureHandler do
114
136
  it "uses default logger if rack logger is nil" do
115
137
 
116
138
  Log4r::Logger['racklogger'].should_receive(:error) do |msg|
117
- msg.should =~ /^RuntimeError: I failed/
139
+ msg.should be_instance_of RuntimeError
140
+ msg.message.should == "I failed"
118
141
  end
119
142
 
120
143
  lambda {
@@ -125,7 +148,8 @@ describe Log4rExceptionable::RackFailureHandler do
125
148
  it "uses default logger if rack logger is not log4r" do
126
149
 
127
150
  Log4r::Logger['racklogger'].should_receive(:error) do |msg|
128
- msg.should =~ /^RuntimeError: I failed/
151
+ msg.should be_instance_of RuntimeError
152
+ msg.message.should == "I failed"
129
153
  end
130
154
 
131
155
  lambda {
@@ -137,7 +161,8 @@ describe Log4rExceptionable::RackFailureHandler do
137
161
  Log4r::Logger.new('RackLogger')
138
162
  Log4r::Logger['racklogger'].should_not_receive(:error)
139
163
  Log4r::Logger['RackLogger'].should_receive(:error) do |msg|
140
- msg.should =~ /^RuntimeError: I failed/
164
+ msg.should be_instance_of RuntimeError
165
+ msg.message.should == "I failed"
141
166
  end
142
167
 
143
168
  lambda {
@@ -52,10 +52,9 @@ describe Log4rExceptionable::ResqueFailureHandler do
52
52
  it "triggers failure handler" do
53
53
 
54
54
  Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
55
- msg.should =~ /^RuntimeError: I failed/
56
- msg.should =~ /resque_failure_handler_spec.rb/
57
- Log4r::MDC.get('resque_exception_line').should =~ /\d+/
58
- Log4r::MDC.get('resque_exception_file').should =~ /resque_failure_handler_spec.rb/
55
+ msg.should be_instance_of RuntimeError
56
+ msg.message.should == "I failed"
57
+ msg.backtrace.first.should =~ /resque_failure_handler_spec.rb/
59
58
  Log4r::MDC.get('resque_worker').should == ""
60
59
  Log4r::MDC.get('resque_queue').should == "somequeue"
61
60
  Log4r::MDC.get('resque_class').should == "SomeJob"
@@ -68,7 +67,8 @@ describe Log4rExceptionable::ResqueFailureHandler do
68
67
  it "uses default logger if job logger is nil" do
69
68
 
70
69
  Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
71
- msg.should =~ /^RuntimeError: I failed/
70
+ msg.should be_instance_of RuntimeError
71
+ msg.message.should == "I failed"
72
72
  Log4r::MDC.get('resque_class').should == "SomeJobWithNilLogger"
73
73
  end
74
74
 
@@ -78,7 +78,8 @@ describe Log4rExceptionable::ResqueFailureHandler do
78
78
  it "uses default logger if job logger is not log4r" do
79
79
 
80
80
  Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
81
- msg.should =~ /^RuntimeError: I failed/
81
+ msg.should be_instance_of RuntimeError
82
+ msg.message.should == "I failed"
82
83
  Log4r::MDC.get('resque_class').should == "SomeJobWithOtherLogger"
83
84
  end
84
85
 
@@ -89,7 +90,8 @@ describe Log4rExceptionable::ResqueFailureHandler do
89
90
  Log4r::Logger.new('SomeJobWithLogger')
90
91
  Log4r::Logger['resquelogger'].should_not_receive(:error)
91
92
  Log4r::Logger['SomeJobWithLogger'].should_receive(:error) do |msg|
92
- msg.should =~ /^RuntimeError: I failed/
93
+ msg.should be_instance_of RuntimeError
94
+ msg.message.should == "I failed"
93
95
  Log4r::MDC.get('resque_class').should == "SomeJobWithLogger"
94
96
  end
95
97
 
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.5.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: