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

TECH-4787: support progname and message block #34

Merged
merged 15 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion lib/contextual_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def add(arg_severity, arg1 = nil, arg2 = nil, **context) # Ruby will prefer to
if arg1.nil?
if block_given?
message = yield
progname = arg2 || context[:progname] || @progname
progname = arg2 || context.delete(:progname) || @progname
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jebentier I added this back as I had it, so that the tunneled progname doesn't live on in the context hash.
Usually I'd avoid delete since that mutates the context here. But it's really succinct, and performs best...and I figured no one else but our code above would be putting a :progname key into the context and calling add in the block form with a context hash! LMK if you disagree.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is okay for now. Maybe something to not in the README is that through this implementation you're able to specify progname using two different method (flip it to a feature 😉 )

logger.info('request') { message } and logger.info(progname: 'request') { message } will pass progname down to the formatter.

Copy link
Contributor Author

@ColinDKelley ColinDKelley Sep 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jebentier Can you take a look at the README restructure here: 549e570

else
message = arg2
progname = @progname
Expand Down
10 changes: 9 additions & 1 deletion spec/lib/contextual_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,16 @@ def expect_log_line_to_be_written(log_line)
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.merge(progname: 'request').to_json)
formatter_args = []
logger.formatter = -> (*args) do
ColinDKelley marked this conversation as resolved.
Show resolved Hide resolved
formatter_args = args
'"message"'
end
expect_log_line_to_be_written('"message"')
expect(logger.info('request', service: 'test_service') { 'this is a test' }).to eq(true)

expect(formatter_args[2]).to eq('request')
expect(formatter_args[3]).to eq(message: "this is a test", service: "test_service")
end
end

Expand Down