class RequestIdLogging::Formatter

A logger formatter which prepends request_id to message.

Constants

DEFAULT_REQ_ID_PROC

Public Class Methods

new(formatter: nil, request_id_proc: DEFAULT_REQ_ID_PROC) click to toggle source

Initialize RequestIdLogging::Formatter

@param [Logger::Formatter] formatter Optional, if you have original formatter, please specify it to this arg.

This formatter prepends request_id to message, and calls your formatter #call method.
If not specified, then message is generated in this formatter.

@param [Proc] request_id_proc Optional, proc object or lambda to customize logged request_id.

If not specified, then raw request_id is used.
Calls superclass method
# File lib/request_id_logging/formatter.rb, line 18
def initialize(formatter: nil, request_id_proc: DEFAULT_REQ_ID_PROC)
  super()
  @original_formatter = formatter
  @req_id_proc = request_id_proc || DEFAULT_PROC
end

Public Instance Methods

call(severity, time, progname, msg) click to toggle source
Calls superclass method
# File lib/request_id_logging/formatter.rb, line 24
def call(severity, time, progname, msg)
  if @original_formatter
    @original_formatter.call(severity, time, progname, new_msg(msg))
  else
    super(severity, time, progname, new_msg(msg))
  end
end

Private Instance Methods

new_msg(msg) click to toggle source
# File lib/request_id_logging/formatter.rb, line 34
def new_msg(msg)
  "[#{request_id}] #{msg2str(msg)}"
end
request_id() click to toggle source
# File lib/request_id_logging/formatter.rb, line 38
def request_id
  id = Thread.current[RequestIdLogging::FIBER_LOCAL_KEY]
  @req_id_proc.call(id)
end