Skip to content
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

Truncating log messages when they are too large #315

Merged
merged 3 commits into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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