Class: RightSupport::Log::TagLogger

Inherits:
FilterLogger show all
Defined in:
lib/right_support/log/tag_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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FilterLogger

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

Constructor Details

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

Instance Attribute Details

#tagObject

Returns the value of attribute tag.



57
58
59
# File 'lib/right_support/log/tag_logger.rb', line 57

def tag
  @tag
end

Instance Method Details

#filter(severity, message) ⇒ Object

Prepend the current tag to the log message; return the same severity and the modified message.

Parameters

severity(Integer)

one of the severity constants defined by Logger

messgae(String)

the log message

Return

Returns a pair consisting of the filtered [severity, message].



47
48
49
50
51
52
53
54
55
# File 'lib/right_support/log/tag_logger.rb', line 47

def filter(severity, message)
  @tls_id ||= "tag_logger_#{self.object_id}"
  tag = Thread.current[@tls_id] || ''
  if tag
    return [severity, tag + message]
  else
    return [severity, message]
  end
end