Use custom output to break long log lines. Force encoding.

This commit is contained in:
Chris Roberts 2021-11-19 17:13:45 -08:00 committed by Paul Hinze
parent 6a1c5f1c7f
commit 8b7c5efa52
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 31 additions and 13 deletions

View File

@ -236,14 +236,15 @@ module Vagrant
def self.enable_server_mode!
if !server_mode?
SERVER_MODE_CALLBACKS.each(&:call)
Log4r::Outputter.stderr.formatter = Util::HCLogFormatter.new
Util::HCLogOutputter.new("hclog")
Log4r::Outputter["hclog"].formatter = Util::HCLogFormatter.new
Log4r::Logger.each_logger do |l|
l.outputters = Log4r::Outputter.stderr if l.parent == Log4r::RootLogger.instance
l.outputters = Log4r::Outputter["hclog"] if l.parent == Log4r::RootLogger.instance
end
Log4r::Logger::Repository.class_eval do
def self.[]=(n, l)
self.synchronize do
l.outputters = Log4r::Outputter.stderr if l.parent == Log4r::RootLogger.instance
l.outputters = Log4r::Outputter["hclog"] if l.parent == Log4r::RootLogger.instance
instance.loggers[n] = l
end
end

View File

@ -19,6 +19,7 @@ module Vagrant
autoload :GuestInspection, 'vagrant/util/guest_inspection'
autoload :HashWithIndifferentAccess, 'vagrant/util/hash_with_indifferent_access'
autoload :HCLogFormatter, 'vagrant/util/logging_formatter'
autoload :HCLogOutputter, 'vagrant/util/logging_formatter'
autoload :InstallShellConfig, 'vagrant/util/install_cli_autocomplete'
autoload :InstallZSHShellConfig, 'vagrant/util/install_cli_autocomplete'
autoload :InstallBashShellConfig, 'vagrant/util/install_cli_autocomplete'

View File

@ -27,18 +27,34 @@ module Vagrant
class HCLogFormatter < Log4r::BasicFormatter
def format(event)
d = {
"@timestamp" => Time.now.strftime("%Y-%m-%dT%H:%M:%S.%6N%:z"),
"@level" => Log4r::LNAMES[event.level].downcase,
"@module" => event.fullname.gsub("::", "."),
"@message" => format_object(event.data),
}
d["@caller"] = event.tracer[0] if event.tracer
message = format_object(event.data).
force_encoding('UTF-8').
scrub("?")
if message.count("\n") > 40
message = message.split("\n").each_slice(40).to_a
message = [message.shift.join("\n")] + message.map { |m|
"continued...\n" + m.join("\n") }
else
message = [message]
end
# TODO(spox): fix this with force encoding on the message
begin
message.map do |msg|
d = {
"@timestamp" => Time.now.strftime("%Y-%m-%dT%H:%M:%S.%6N%:z"),
"@level" => Log4r::LNAMES[event.level].downcase,
"@module" => event.fullname.gsub("::", "."),
"@message" => msg,
}
d["@caller"] = event.tracer[0] if event.tracer
d.to_json + "\n"
rescue
end
end
end
class HCLogOutputter < Log4r::StderrOutputter
def write(data)
data.each do |d|
super(d)
end
end
end