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

Ensure all strings with spaces are quoted in logs #332

Merged
merged 4 commits into from
Jan 30, 2020

Conversation

bensymonds
Copy link
Contributor

The current logic covers the case when the log data is already a String instance, but not when the data is some other object that, when interpolated into the log string, results in a string containing spaces.

The example that led me here is where an exception is logged and the exception's message contains spaces. The exception gets interpolated into the string and has to_s called on it. Exception#to_s returns the message, so the message (with its spaces) go into the log line, but it's bypassed the logic that adds quotes.

See the commits for more details. I slipped in a few cleanups and added some tests too.

I think the code is obvious enough here.
The problem with the stubbing is that it allows the implementation to output extra _unexpected_ stuff, in addition to what the test expects, without causing a test failure. By removing it we ensure a test will fail if unexpected stuff is output.
I couldn't see explicit tests for this stuff anywhere (some of it is indirectly tested higher up in the file).
The current logic covers the case when the log data is already a `String` instance, but not when the data is some other object that, when interpolated into the log string, results in a string containing spaces.

The example that led me here is where an exception is logged and the exception's message contains spaces. The exception gets interpolated into the string and has `to_s` called on it. [`Exception#to_s`](https://ruby-doc.org/core-2.6.5/Exception.html#method-i-to_s) returns the message, so the message (with its spaces) go into the log line, but it's bypassed the logic that adds quotes.

I used `"#{v}"` instead of `v.to_s` because it seems there is a [subtle difference in their behaviour](https://github.com/ruby/spec/blob/master/language/string_spec.rb#L147-L158), to cover the rare case where `to_s` doesn't return a string. I think we want to keep the existing behaviour here.
@bensymonds
Copy link
Contributor Author

@stevenharman Thanks for the review. FYI I amended the last commit to add a changelog entry. I'm going to go ahead and merge this. I actually have another PR I want to open, but after that I'll be bugging you for a release 😄

@bensymonds bensymonds merged commit 3a76af1 into master Jan 30, 2020
@bensymonds bensymonds deleted the quote-more-strings branch January 30, 2020 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants