Class: RightSupport::Log::ExceptionLogger

Inherits:
FilterLogger show all
Defined in:
lib/right_support/log/exception_logger.rb

Overview

A logger that prepends a tag to every message that is emitted. Can be used to correlate logs with a Web session ID, transaction ID or other context.

The user of this logger is responsible for calling #tag= to set the tag as appropriate, e.g. in a Web request around-filter.

This logger uses thread-local storage (TLS) to provide tagging on a per-thread basis; however, it does not account for EventMachine, neverblock, the use of Ruby fibers, or any other phenomenon that can “hijack” a thread’s call stack.

Constant Summary

Constants inherited from FilterLogger

FilterLogger::SEVERITY_TO_METHOD

Instance Attribute Summary

Attributes inherited from FilterLogger

#actual_logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FilterLogger

#<<, #add, #close, #debug, #debug?, #error, #error?, #fatal, #fatal?, #info, #info?, #initialize, #level, #level=, #method_missing, #respond_to?, #warn, #warn?

Constructor Details

This class inherits a constructor from RightSupport::Log::FilterLogger

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RightSupport::Log::FilterLogger

Class Method Details

.format_exception(description, exception = nil, backtrace = :caller) ⇒ Object

Format exception information

Parameters

description(String)

Error description

exception(Exception|String)

Associated exception or other parenthetical error information

backtrace(Symbol)

Exception backtrace extent: :no_trace, :caller, or :trace,

defaults to :caller

Return

(String)

Information about the exception in a format suitable for logging



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
# File 'lib/right_support/log/exception_logger.rb', line 45

def self.format_exception(description, exception = nil, backtrace = :caller)
  if exception
    if exception.respond_to?(:message)
      description += " (#{exception.class}: #{exception.message}"
    else
      description += " (#{exception}"
    end

    unless exception.respond_to?(:backtrace) && exception.backtrace
      backtrace = :no_trace
    end

    case backtrace
      when :no_trace
        description += ")"
      when :caller
        description += " IN " + exception.backtrace[0] + ")"
      when :trace
        description += " IN\n  " + exception.backtrace.join("\n  ") + ")"
      else
        raise ArgumentError, "Unknown backtrace value #{backtrace.inspect}"
    end
  end

  description
end

Instance Method Details

#exception(description, exception = nil, backtrace = :caller) ⇒ Object

Log information about an exception with ERROR severity.

Parameters

description(String)

Error description

exception(Exception|String)

Associated exception or other parenthetical error information

backtrace(Symbol)

Exception backtrace extent: :no_trace, :caller, or :trace,

defaults to :caller

Return

Forwards the return value of its underlying logger’s #error method



82
83
84
# File 'lib/right_support/log/exception_logger.rb', line 82

def exception(description, exception = nil, backtrace = :caller)
  error(self.class.format_exception(description, exception, backtrace))
end