Skip to content

Commit

Permalink
Merge pull request #315 from juliancheal/fix_logger_size_limit
Browse files Browse the repository at this point in the history
Truncating log messages when they are too large
  • Loading branch information
Fryguy authored Jan 4, 2018
2 parents e04c65f + 3f16029 commit d09f86a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/gems/pending/util/vmdb-logger.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions spec/util/vmdb-logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down

0 comments on commit d09f86a

Please sign in to comment.