From ad4fb78fa1ab20041e4c4c0553a0062b49eb1d8a Mon Sep 17 00:00:00 2001 From: Julian Cheal Date: Tue, 21 Nov 2017 18:21:00 +0000 Subject: [PATCH 1/3] Truncating log messages when they are too large --- lib/gems/pending/util/vmdb-logger.rb | 3 ++- spec/util/vmdb-logger_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/gems/pending/util/vmdb-logger.rb b/lib/gems/pending/util/vmdb-logger.rb index 10e1767ee..a4b27e98d 100644 --- a/lib/gems/pending/util/vmdb-logger.rb +++ b/lib/gems/pending/util/vmdb-logger.rb @@ -156,8 +156,9 @@ 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)) + max_log_line_length = 1.megabyte + 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..34ccdae22 100644 --- a/spec/util/vmdb-logger_spec.rb +++ b/spec/util/vmdb-logger_spec.rb @@ -95,6 +95,12 @@ expect(VMDBLogger.contents("mylog.log")).to eq("") end + context "long messages" do + it "truncates long messages when max_message_size is set" do + # TODO Add test body! + 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")) From 921f6945f24e5c4f47b43639e274df089120168b Mon Sep 17 00:00:00 2001 From: Julian Cheal Date: Tue, 21 Nov 2017 19:22:11 +0000 Subject: [PATCH 2/3] Added spec test to check msg is truncated to 1.megabyte --- spec/util/vmdb-logger_spec.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/util/vmdb-logger_spec.rb b/spec/util/vmdb-logger_spec.rb index 34ccdae22..3dbc3c609 100644 --- a/spec/util/vmdb-logger_spec.rb +++ b/spec/util/vmdb-logger_spec.rb @@ -96,8 +96,14 @@ end context "long messages" do + before(:each) do + @logger = VMDBLogger.new(@log) + end + it "truncates long messages when max_message_size is set" do - # TODO Add test body! + 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 From 3f16029dbbee4587a3b658f8f4d0831e4eaa70b1 Mon Sep 17 00:00:00 2001 From: Julian Cheal Date: Tue, 21 Nov 2017 21:30:30 +0000 Subject: [PATCH 3/3] Added ActiveSupport#String Allows the tests and gems-pending code to be run standalone. Changed before(:each) to simple let(:foo) in specs MAX_LOG_LINE_LENGTH is a constant --- lib/gems/pending/util/vmdb-logger.rb | 8 +++++--- spec/util/vmdb-logger_spec.rb | 6 ++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/gems/pending/util/vmdb-logger.rb b/lib/gems/pending/util/vmdb-logger.rb index a4b27e98d..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,9 +160,7 @@ class Formatter < Logger::Formatter FORMAT = "[----] %s, [%s#%d:%x] %5s -- %s: %s\n" def call(severity, time, progname, msg) - max_log_line_length = 1.megabyte - - msg = prefix_task_id(msg2str(msg)).truncate(max_log_line_length) + 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 3dbc3c609..62098c3f9 100644 --- a/spec/util/vmdb-logger_spec.rb +++ b/spec/util/vmdb-logger_spec.rb @@ -96,13 +96,11 @@ end context "long messages" do - before(:each) do - @logger = VMDBLogger.new(@log) - end + 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("-- : ") + _, message = logger.formatter.call(:error, Time.now.utc, "", msg).split("-- : ") expect(message.strip.size).to eq(1.megabyte) end end