diff --git a/lib/gems/pending/util/vmdb-logger.rb b/lib/gems/pending/util/vmdb-logger.rb index 10e1767ee..9dff29e40 100644 --- a/lib/gems/pending/util/vmdb-logger.rb +++ b/lib/gems/pending/util/vmdb-logger.rb @@ -1,8 +1,12 @@ require 'logger' require 'active_support/core_ext/object/try' +require 'active_support/core_ext/numeric/bytes' +require 'active_support/core_ext/string' require 'English' class VMDBLogger < Logger + MAX_LOG_LINE_LENGTH = 1.megabyte + def initialize(*args) super self.level = INFO @@ -156,8 +160,7 @@ class Formatter < Logger::Formatter FORMAT = "[----] %s, [%s#%d:%x] %5s -- %s: %s\n" def call(severity, time, progname, msg) - msg = prefix_task_id(msg2str(msg)) - + msg = prefix_task_id(msg2str(msg)).truncate(MAX_LOG_LINE_LENGTH) FORMAT % [severity[0..0], format_datetime(time), $PROCESS_ID, Thread.current.object_id, severity, progname, msg] end diff --git a/spec/util/vmdb-logger_spec.rb b/spec/util/vmdb-logger_spec.rb index efaba994f..62098c3f9 100644 --- a/spec/util/vmdb-logger_spec.rb +++ b/spec/util/vmdb-logger_spec.rb @@ -95,6 +95,16 @@ expect(VMDBLogger.contents("mylog.log")).to eq("") end + context "long messages" do + let(:logger) { VMDBLogger.new(@log) } + + it "truncates long messages when max_message_size is set" do + msg = "a" * 1_572_864 # 1.5 mb in bytes + _, message = logger.formatter.call(:error, Time.now.utc, "", msg).split("-- : ") + expect(message.strip.size).to eq(1.megabyte) + end + end + context "with evm log snippet with invalid utf8 byte sequence data" do before(:each) do @log = File.expand_path(File.join(File.dirname(__FILE__), "data/redundant_utf8_byte_sequence.log"))