Class: Dclog::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/dclog/formatter.rb

Constant Summary collapse

LOG_REGEX =
/^\[([a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})\]\s+(.+)$/.freeze

Instance Method Summary collapse

Constructor Details

#initializeFormatter

Returns a new instance of Formatter.



6
7
8
# File 'lib/dclog/formatter.rb', line 6

def initialize
  @silencers = []
end

Instance Method Details

#add_silencer(&block) ⇒ Object



10
11
12
# File 'lib/dclog/formatter.rb', line 10

def add_silencer(&block)
  @silencers << block
end

#call(severity, timestamp, progname, message) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dclog/formatter.rb', line 14

def call(severity, timestamp, progname, message)
  return if should_filter?(message)

  msg_regex = message.match(LOG_REGEX)
  request_id = msg_regex.nil? ? nil : msg_regex[1]
  msg = msg_regex.nil? ? message : msg_regex[2]

  "#{JSON.dump(
    severity: severity,
    date: timestamp.strftime("%Y-%m-%d %H:%M:%S"),
    caller: progname,
    request_id: request_id,
    message: msg
  )}\n"
end