log4r-exceptionable 0.5.0 → 0.5.1

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.5.1 (06/15/2012)
2
+ ------------------
3
+
4
+ Added source logger detection and use <ccfba07>
5
+
1
6
  0.5.0
2
7
  -----
3
8
 
data/README.md CHANGED
@@ -17,15 +17,28 @@ To use
17
17
  Add to some initializer code:
18
18
 
19
19
  Log4rExceptionable::Configuration.configure do |config|
20
- # at least one logger needs to be set
20
+
21
+ # The default loggers used when a source logger is not available
22
+ # required (at least one logger needs to be set, but you don't need
23
+ # to set rack if using resque, and vice versa)
24
+ #
21
25
  config.rack_failure_logger = "rails::SomeRackLogger"
22
26
  config.resque_failure_logger = "rails::SomeResqueLogger"
27
+
28
+ # Prevent the detection and use of the source class's logger
29
+ # The source logger is taken from the resque class raising
30
+ # the exception, rails controller raising the exception,
31
+ # or rack_env['rack.logger']
32
+ # optional (defaults to true)
33
+ #
34
+ # config.use_source_logger = false
35
+
23
36
  end
24
37
 
25
38
  Rails.application.config.middleware.use "Log4rExceptionable::RackFailureHandler"
26
39
  Resque::Failure.backend = Log4rExceptionable::ResqueFailureHandler
27
40
 
28
- All failures will be logged using the given log4r logger name (or log4r logger instance if supplied instead)
41
+ All failures will be logged using the source logger or the given logger if a source is not available. The logger can be set using their log4r fullname or a log4r logger instance.
29
42
 
30
43
  Author
31
44
  ------
@@ -1,26 +1,24 @@
1
1
  module Log4rExceptionable
2
2
 
3
- # Failure backends that log exceptions with log4r
4
- #
5
- # Log4rExceptionable::Configuration.configure do |config|
6
- # # required
7
- # config.resque_failure_logger = "rails::SomeLogger"
8
- # config.rack_failure_logger = "rails::SomeLogger"
9
- # end
10
- #
11
- # Rails.application.config.middleware.use "Log4rExceptionable::RackFailureHandler"
12
- # Resque::Failure.backend = Log4rExceptionable::ResqueFailureHandler
3
+ # Configuration for the failure backends that log exceptions with log4r
13
4
  #
14
5
  class Configuration
15
6
 
16
7
  class << self
17
- # required
8
+ # required - default loggers used if source logger not available
18
9
  attr_accessor :rack_failure_logger, :resque_failure_logger
10
+ attr_accessor :use_source_logger
19
11
  end
20
12
 
13
+ # default values
14
+ self.use_source_logger = true
15
+
21
16
  def self.configure
22
17
  yield self
23
- raise "log4r-exceptionable requires a rack_failure_logger or resque_failure_logger" unless self.rack_failure_logger || self.resque_failure_logger
18
+
19
+ if ! self.rack_failure_logger && ! self.resque_failure_logger
20
+ raise "log4r-exceptionable requires a rack_failure_logger or resque_failure_logger"
21
+ end
24
22
 
25
23
  if self.rack_failure_logger
26
24
  self.set_logger(:rack_failure_logger)
@@ -10,10 +10,6 @@ module Log4rExceptionable
10
10
  @app = app
11
11
  end
12
12
 
13
- def logger
14
- Log4rExceptionable::Configuration.rack_failure_logger
15
- end
16
-
17
13
  def call(env)
18
14
  # Make thread safe
19
15
  dup._call(env)
@@ -62,8 +58,17 @@ module Log4rExceptionable
62
58
  end
63
59
  end
64
60
 
61
+ controller = env['action_controller.instance']
62
+ if controller && controller.respond_to?(:logger) && controller.logger.instance_of?(Log4r::Logger)
63
+ error_logger = controller.logger
64
+ elsif env['rack.logger'] && env['rack.logger'].instance_of?(Log4r::Logger)
65
+ error_logger = env['rack.logger']
66
+ else
67
+ error_logger = Log4rExceptionable::Configuration.rack_failure_logger
68
+ end
69
+
65
70
  message = "#{exception.class}: #{exception.message}"
66
- logger.error(message)
71
+ error_logger.error(message)
67
72
  ensure
68
73
  # Since this is somewhat of a global map, clean the keys
69
74
  # we put in so other log messages don't see them
@@ -6,10 +6,6 @@ module Log4rExceptionable
6
6
  #
7
7
  class ResqueFailureHandler < ::Resque::Failure::Base
8
8
 
9
- def logger
10
- Log4rExceptionable::Configuration.resque_failure_logger
11
- end
12
-
13
9
  def save
14
10
  begin
15
11
  mdc = Log4r::MDC
@@ -29,9 +25,16 @@ module Log4rExceptionable
29
25
  mdc.put("resque_queue", queue.to_s)
30
26
  mdc.put("resque_class", payload['class'].to_s)
31
27
  mdc.put("resque_args", payload['args'].inspect.to_s)
28
+
29
+ payload_class = Resque.constantize(payload['class']) rescue nil
30
+ if payload_class && payload_class.respond_to?(:logger) && payload_class.logger.instance_of?(Log4r::Logger)
31
+ error_logger = payload_class.logger
32
+ else
33
+ error_logger = Log4rExceptionable::Configuration.resque_failure_logger
34
+ end
32
35
 
33
36
  message = "#{exception.class}: #{exception.message}"
34
- logger.error(message)
37
+ error_logger.error(message)
35
38
  ensure
36
39
  # Since this is somewhat of a global map, clean the keys
37
40
  # we put in so other log messages don't see them
@@ -1,3 +1,3 @@
1
1
  module Log4rExceptionable
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -3,13 +3,46 @@ require "spec_helper"
3
3
  describe Log4rExceptionable::RackFailureHandler do
4
4
  include Rack::Test::Methods
5
5
 
6
+ class FakeController
7
+ class << self
8
+ attr_accessor :logger
9
+ end
10
+ def logger
11
+ self.class.logger
12
+ end
13
+ end
14
+
6
15
  class TestApp
16
+
7
17
  def call(env)
8
- if env['PATH_INFO'] =~ /error/
9
- raise "I failed"
10
- else
11
- return [200, {"Content-Type" => "text"}, ["hello"]]
18
+ error = env['PATH_INFO'] =~ /error/
19
+
20
+ case env['PATH_INFO']
21
+ when /nil_controller_logger/
22
+ FakeController.logger = nil
23
+ env['action_controller.instance'] = FakeController.new
24
+ error = true
25
+ when /other_controller_logger/
26
+ FakeController.logger = Object.new
27
+ env['action_controller.instance'] = FakeController.new
28
+ error = true
29
+ when /controller_logger/
30
+ FakeController.logger = Log4r::Logger["ControllerLogger"] || Log4r::Logger.new("ControllerLogger")
31
+ env['action_controller.instance'] = FakeController.new
32
+ error = true
33
+ when /nil_rack_logger/
34
+ env['rack.logger'] = nil
35
+ error = true
36
+ when /other_rack_logger/
37
+ env['rack.logger'] = Object.new
38
+ error = true
39
+ when /rack_logger/
40
+ env['rack.logger'] = Log4r::Logger["RackLogger"] || Log4r::Logger.new("RackLogger")
41
+ error = true
12
42
  end
43
+
44
+ raise "I failed" if error
45
+ return [200, {"Content-Type" => "text"}, ["hello"]]
13
46
  end
14
47
  end
15
48
 
@@ -45,6 +78,74 @@ describe Log4rExceptionable::RackFailureHandler do
45
78
  }.should raise_error("I failed")
46
79
  end
47
80
 
81
+ it "uses default logger if controller logger is nil" do
82
+
83
+ Log4r::Logger['racklogger'].should_receive(:error) do |msg|
84
+ msg.should == "RuntimeError: I failed"
85
+ end
86
+
87
+ lambda {
88
+ get "/nil_controller_logger"
89
+ }.should raise_error("I failed")
90
+ end
91
+
92
+ it "uses default logger if controller logger is not log4r" do
93
+
94
+ Log4r::Logger['racklogger'].should_receive(:error) do |msg|
95
+ msg.should == "RuntimeError: I failed"
96
+ end
97
+
98
+ lambda {
99
+ get "/other_controller_logger"
100
+ }.should raise_error("I failed")
101
+ end
102
+
103
+ it "uses controller logger if set" do
104
+ Log4r::Logger.new('ControllerLogger')
105
+ Log4r::Logger['racklogger'].should_not_receive(:error)
106
+ Log4r::Logger['ControllerLogger'].should_receive(:error) do |msg|
107
+ msg.should == "RuntimeError: I failed"
108
+ end
109
+
110
+ lambda {
111
+ get "/controller_logger"
112
+ }.should raise_error("I failed")
113
+ end
114
+
115
+ it "uses default logger if rack logger is nil" do
116
+
117
+ Log4r::Logger['racklogger'].should_receive(:error) do |msg|
118
+ msg.should == "RuntimeError: I failed"
119
+ end
120
+
121
+ lambda {
122
+ get "/nil_rack_logger"
123
+ }.should raise_error("I failed")
124
+ end
125
+
126
+ it "uses default logger if rack logger is not log4r" do
127
+
128
+ Log4r::Logger['racklogger'].should_receive(:error) do |msg|
129
+ msg.should == "RuntimeError: I failed"
130
+ end
131
+
132
+ lambda {
133
+ get "/other_rack_logger"
134
+ }.should raise_error("I failed")
135
+ end
136
+
137
+ it "uses rack logger if set" do
138
+ Log4r::Logger.new('RackLogger')
139
+ Log4r::Logger['racklogger'].should_not_receive(:error)
140
+ Log4r::Logger['RackLogger'].should_receive(:error) do |msg|
141
+ msg.should == "RuntimeError: I failed"
142
+ end
143
+
144
+ lambda {
145
+ get "/rack_logger"
146
+ }.should raise_error("I failed")
147
+ end
148
+
48
149
  end
49
150
 
50
151
  end
@@ -11,6 +11,36 @@ describe Log4rExceptionable::ResqueFailureHandler do
11
11
  end
12
12
  end
13
13
 
14
+ class SomeJobWithNilLogger
15
+ def self.logger
16
+ nil
17
+ end
18
+
19
+ def self.perform(*args)
20
+ raise "I failed"
21
+ end
22
+ end
23
+
24
+ class SomeJobWithOtherLogger
25
+ def self.logger
26
+ self
27
+ end
28
+
29
+ def self.perform(*args)
30
+ raise "I failed"
31
+ end
32
+ end
33
+
34
+ class SomeJobWithLogger
35
+ def self.logger
36
+ Log4r::Logger["SomeJobWithLogger"] || Log4r::Logger.new("SomeJobWithLogger")
37
+ end
38
+
39
+ def self.perform(*args)
40
+ raise "I failed"
41
+ end
42
+ end
43
+
14
44
  before(:all) do
15
45
  Log4rExceptionable::Configuration.configure do |config|
16
46
  config.resque_failure_logger = 'resquelogger'
@@ -36,6 +66,37 @@ describe Log4rExceptionable::ResqueFailureHandler do
36
66
  run_resque_job(SomeJob, 'foo', :queue => :somequeue, :inline => true)
37
67
  end
38
68
 
69
+ it "uses default logger if job logger is nil" do
70
+
71
+ Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
72
+ msg.should == "RuntimeError: I failed"
73
+ Log4r::MDC.get('resque_class').should == "SomeJobWithNilLogger"
74
+ end
75
+
76
+ run_resque_job(SomeJobWithNilLogger, 'foo', :queue => :somequeue, :inline => true)
77
+ end
78
+
79
+ it "uses default logger if job logger is not log4r" do
80
+
81
+ Log4r::Logger['resquelogger'].should_receive(:error) do |msg|
82
+ msg.should == "RuntimeError: I failed"
83
+ Log4r::MDC.get('resque_class').should == "SomeJobWithOtherLogger"
84
+ end
85
+
86
+ run_resque_job(SomeJobWithOtherLogger, 'foo', :queue => :somequeue, :inline => true)
87
+ end
88
+
89
+ it "uses job logger if set" do
90
+ Log4r::Logger.new('SomeJobWithLogger')
91
+ Log4r::Logger['resquelogger'].should_not_receive(:error)
92
+ Log4r::Logger['SomeJobWithLogger'].should_receive(:error) do |msg|
93
+ msg.should == "RuntimeError: I failed"
94
+ Log4r::MDC.get('resque_class').should == "SomeJobWithLogger"
95
+ end
96
+
97
+ run_resque_job(SomeJobWithLogger, 'foo', :queue => :somequeue, :inline => true)
98
+ end
99
+
39
100
  end
40
101
 
41
102
  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.5.0
4
+ version: 0.5.1
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-14 00:00:00.000000000 Z
12
+ date: 2012-06-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake