-
Notifications
You must be signed in to change notification settings - Fork 898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logging to STDOUT in JSON format for containers #15392
Changes from all commits
97ff263
917a426
d049179
3a968f7
778fc12
5f76088
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Vmdb::Loggers | ||
class ContainerLogger < VMDBLogger | ||
def initialize(logdev = STDOUT, *args) | ||
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 < VMDBLogger::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 call(severity, time, progname, msg) | ||
# From https://github.com/ViaQ/elasticsearch-templates/releases Downloads asciidoc | ||
{ | ||
:@timestamp => format_datetime(time), | ||
:hostname => hostname, | ||
:level => translate_error(severity), | ||
:message => prefix_task_id(msg2str(msg)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't this need a PR to gems pending? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
:pid => $PROCESS_ID, | ||
:tid => thread_id, | ||
:service => progname, | ||
# :tags => "tags string", | ||
}.to_json << "\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 | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
class MulticastLogger < Logger | ||
attr_accessor :loggers | ||
|
||
def initialize(*loggers) | ||
require 'set' | ||
@loggers = Set.new(loggers) | ||
@level = DEBUG | ||
end | ||
|
||
def level=(new_level) | ||
loggers.each { |l| l.level = new_level } | ||
super | ||
end | ||
|
||
def filename | ||
loggers.first.filename | ||
end | ||
|
||
[:log_backtrace, :log_hashes].each do |method| | ||
define_method(method) do |*args| | ||
loggers.map { |l| l.send(method, *args) }.first | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note, this implies all loggers passed in through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we care right now. We only have one place using it and it is always backed by one of our loggers. |
||
end | ||
end | ||
|
||
def add(*args, &block) | ||
severity = args.first || UNKNOWN | ||
return true if severity < @level | ||
loggers.each { |l| l.send(:add, *args, &block) } | ||
true | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
describe Vmdb::Loggers::ContainerLogger::Formatter do | ||
it "stuff" do | ||
time = Time.now | ||
result = described_class.new.call("INFO", time, "some_program", "testing 1, 2, 3") | ||
expect(JSON.parse(result)).to have_attributes( | ||
"@timestamp" => time.strftime("%Y-%m-%dT%H:%M:%S.%6N "), | ||
"hostname" => ENV["HOSTNAME"], | ||
"level" => "info", | ||
"message" => "testing 1, 2, 3", | ||
"pid" => $PROCESS_ID, | ||
"service" => "some_program", | ||
"tid" => Thread.current.object_id.to_s(16), | ||
) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
describe MulticastLogger do | ||
let(:logger1) { Logger.new(StringIO.new) } | ||
let(:logger2) { Logger.new(StringIO.new) } | ||
subject { described_class.new(logger1, logger2) } | ||
|
||
context "#add" do | ||
it "forwards to the other loggers" do | ||
expect(logger1).to receive(:add).with(1, nil, "test message") | ||
expect(logger2).to receive(:add).with(1, nil, "test message") | ||
|
||
subject.info("test message") | ||
end | ||
|
||
it "only forwards the message if the severity is correct" do | ||
subject.level = 1 | ||
logger1.level = 0 | ||
|
||
[logger1, logger2].each { |l| expect(l).not_to receive(:add) } | ||
|
||
subject.debug("test message") | ||
end | ||
end | ||
|
||
it "#level= updates the log level on all backing devices" do | ||
[logger1, logger2, subject].each { |l| expect(l.level).to eq(0) } | ||
subject.level = 3 | ||
[logger1, logger2, subject].each { |l| expect(l.level).to eq(3) } | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to follow this URL, but I can't find what you are trying to point to here. What is the
Downloads asciidoc
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Follow the link, under "Downloads" you'll see an asciidoc which contains the "schema"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh...that wasn't clear to me, as it was a full java-namespace looking thing