-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkey_value.rb
More file actions
37 lines (36 loc) · 1.19 KB
/
key_value.rb
File metadata and controls
37 lines (36 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true
module MrLogaLoga
module Formatters
# == Description
#
# A simple key value formatter that extends the standard formatter by rendering additional contextual information.
#
# == Format
#
# The key-value formatter renders messages into the following format:
#
# Log format:
#
# SeverityID, [DateTime #pid] SeverityLabel -- ProgName: message key1=value1 key2=value2
#
class KeyValue < Logger::Formatter
# Render a log message
#
# @param severity [String] The message severity
# @param datetime [DateTime] The message date time
# @param progname [DateTime] The program name
# @param message [String] The log message
# @param context [Hash] The log message context
#
# @return [String] the formatted log message
def call(severity, datetime, progname, message, context = {})
message = message ? msg2str(message).strip : ''
message = context.map { |key, value| "#{key}=#{value}" }
.prepend(message)
.compact
.join(' ')
super(severity, datetime, progname, message)
end
end
end
end