-
Notifications
You must be signed in to change notification settings - Fork 18
/
container.rb
78 lines (67 loc) · 2.29 KB
/
container.rb
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
module ManageIQ
module Loggers
class Container < Base
def initialize(logdev = STDOUT, *args)
logdev.sync = true # Don't buffer container log output
super
self.level = DEBUG
self.formatter = Formatter.new
end
def level=(_new_level)
super(DEBUG) # We want everything written to the ContainerLogger written to STDOUT
end
def filename
"STDOUT"
end
class Formatter < Base::Formatter
SEVERITY_MAP = {
"DEBUG" => "debug",
"INFO" => "info",
"WARN" => "warning",
"ERROR" => "err",
"FATAL" => "crit",
"UNKNOWN" => "unknown"
# Others that don't match up: alert emerg notice trace
}.freeze
def initialize
super
require 'json'
end
def call(severity, time, progname, msg)
# From https://github.com/ViaQ/elasticsearch-templates/releases -> Downloads -> *.asciidoc
# NOTE: These values are in a specific order for easier human readbility via STDOUT
payload = {
:@timestamp => format_datetime(time),
:hostname => hostname,
:pid => $PROCESS_ID,
:tid => thread_id,
:service => progname,
:level => translate_error(severity),
:message => prefix_task_id(msg2str(msg)),
:request_id => request_id
# :tags => "tags string",
}.compact
JSON.generate(payload) << "\n"
end
private
def hostname
@hostname ||= ENV["HOSTNAME"]
end
def thread_id
Thread.current.object_id.to_s(16)
end
def translate_error(level)
SEVERITY_MAP[level] || "unknown"
end
def request_id
Thread.current[:request_id] || Thread.current[:current_request]&.request_id.tap do |request_id|
if request_id
require "active_support/deprecation"
ActiveSupport::Deprecation.warn("Usage of `Thread.current[:current_request]&.request_id` will be deprecated in version 0.5.0. Please switch to `Thread.current[:request_id]` to log request_id automatically.")
end
end
end
end
end
end
end