Class: Log4rExceptionable::RackFailureHandler

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/log4r-exceptionable/rack_failure_handler.rb

Overview

A rack middleware handler that logs exceptions with log4r

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#add_context, #log_with_context

Constructor Details

#initialize(app, opts = {}) ⇒ RackFailureHandler

Returns a new instance of RackFailureHandler.



12
13
14
# File 'lib/log4r-exceptionable/rack_failure_handler.rb', line 12

def initialize(app, opts = {})
  @app = app
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/log4r-exceptionable/rack_failure_handler.rb', line 10

def args
  @args
end

Instance Method Details

#_call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/log4r-exceptionable/rack_failure_handler.rb', line 21

def _call(env)
  begin
    # Call the app we are monitoring
    response = @app.call(env)
  rescue => exception
    # An exception has been raised. Send to log4r
    send_to_log4r(exception, env)
  
    # Raise the exception again to pass back to app.
    raise
  end
  
  if env['rack.exception']
    send_to_log4r(env['rack.exception'], env)
  end
  
  response
end

#call(env) ⇒ Object



16
17
18
19
# File 'lib/log4r-exceptionable/rack_failure_handler.rb', line 16

def call(env)
  # Make thread safe
  dup._call(env)
end

#send_to_log4r(exception, env = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/log4r-exceptionable/rack_failure_handler.rb', line 40

def send_to_log4r(exception, env=nil)
  
  log_with_context do |context|
    
    # add rack env to context so our logger can report with that data 
    if env and env.size > 0
      env.each do |k, v|
        begin
          add_context(context, "rack_env_#{k}", v)
        rescue => e
          $stderr.puts "Log4r Exceptionable could not extract a rack env item: " + e.message
        end
      end
    end

    # Determine exception source class if possible, and use its logger if configured to do so.
    error_logger = nil
    if Log4rExceptionable::Configuration.use_source_logger
      controller = env['action_controller.instance']
      if controller && controller.respond_to?(:logger) && controller.logger.instance_of?(Log4r::Logger)
        error_logger = controller.logger 
        begin
          add_context(context, "rack_controller_name", controller.controller_name)
          add_context(context, "rack_action_name", controller.action_name)
        rescue => e
          $stderr.puts "Log4r Exceptionable could not extract controller names: " + e.message
        end
      elsif env['rack.logger'] && env['rack.logger'].instance_of?(Log4r::Logger)
        error_logger = env['rack.logger']
      end
    end
    
    error_logger ||= Log4rExceptionable::Configuration.rack_failure_logger
    
    error_logger.send(Log4rExceptionable::Configuration.log_level, exception)
    
  end
  
end