Class: RequestIdLogging::Formatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/request_id_logging/formatter.rb

Overview

A logger formatter which prepends request_id to message.

Constant Summary collapse

DEFAULT_REQ_ID_PROC =
->(id) { id }

Instance Method Summary collapse

Constructor Details

#initialize(formatter: nil, request_id_proc: DEFAULT_REQ_ID_PROC) ⇒ Formatter

Initialize RequestIdLogging::Formatter

Parameters:

  • formatter (Logger::Formatter) (defaults to: nil)

    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.

  • request_id_proc (Proc) (defaults to: DEFAULT_REQ_ID_PROC)

    Optional, proc object or lambda to customize logged request_id. If not specified, then raw request_id is used.



18
19
20
21
22
# 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

Instance Method Details

#call(severity, time, progname, msg) ⇒ Object



24
25
26
27
28
29
30
# 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