Skip to content

Commit

Permalink
TECH-4787: add test for inline context and no progname; add support f…
Browse files Browse the repository at this point in the history
…or all 3: progname, context, message block
  • Loading branch information
ColinDKelley committed Aug 28, 2020
1 parent aec156c commit 8cb6574
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
26 changes: 18 additions & 8 deletions lib/contextual_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,29 @@ def current_context_for_thread
Context::Handler.current_context
end

# In methods below, we assume that presence of context means new code that is aware of
# ContextualLogger...and that that code never uses progname.
# In the methods generated below, we assume that presence of context means new code that is
# aware of ContextualLogger...and that that code never uses progname.
# This is important because we only get 2 args total passed to add(), in order to be
# compatible with classic implementations like in the plain Logger
# and ActiveSupport::Logger.broadcast.
# compatible with classic implementations like in the plain Logger and
# ActiveSupport::Logger.broadcast.

# Note that we can't yield before add because `add` might skip it based on log_level. And we can't check
# log_level here because we might be running in ActiveSupport::Logging.broadcast which has multiple
# loggers, each with their own log_level.

LOG_LEVEL_NAMES_TO_SEVERITY.each do |method_name, log_level|
class_eval(<<~EOS, __FILE__, __LINE__ + 1)
def #{method_name}(arg = nil, context = nil, &block)
if context
add(#{log_level}, arg, context, &block)
else
def #{method_name}(arg = nil, **context, &block)
if context.empty?
add(#{log_level}, nil, arg, &block)
else
if arg.nil?
add(#{log_level}, nil, context, &block)
elsif block
add(#{log_level}, nil, context) { "\#{arg}: \#{block.call}" }
else
add(#{log_level}, arg, context)
end
end
end
EOS
Expand Down
25 changes: 21 additions & 4 deletions spec/lib/contextual_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,35 @@ def expect_log_line_to_be_written(log_line)
end

context 'inline context' do
let(:message) { 'this is a test' }
let(:expected_log_hash) do
{
message: 'this is a test',
timestamp: Time.now
severity: 'INFO',
message: message,
timestamp: Time.now,
service: 'test_service'
}
end

it 'handles message block (inline context)' do
expect_log_line_to_be_written(expected_log_hash.merge(severity: 'INFO', service: 'test_service').to_json)
it 'handles message block (inline context) with nil progname' do
expect_log_line_to_be_written(expected_log_hash.to_json)
expect(logger.info(nil, service: 'test_service') { 'this is a test' }).to eq(true)
end

it 'handles message block (inline context) with no progname' do
expect_log_line_to_be_written(expected_log_hash.to_json)
expect(logger.info(service: 'test_service') { 'this is a test' }).to eq(true)
end

context 'with progname and message' do
let(:message) { 'request: this is a test' }

it 'handles message block (inline context) with progname' do
expect_log_line_to_be_written(expected_log_hash.to_json)
expect(logger.info('request', service: 'test_service') { 'this is a test' }).to eq(true)
end
end

context "when log level isn't enabled" do
before { logger.level = Logger::Severity::UNKNOWN }

Expand Down

0 comments on commit 8cb6574

Please sign in to comment.