Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
qa: parametrize logger usages #5173
qa: parametrize logger usages #5173
Changes from 1 commit
f70b5d7
39da317
9076613
f6f1ad5
d1e3677
2d02c21
75c9521
9202869
e2fa44e
e76f9d5
28b87ac
26cd0ea
f9b8e44
b168c19
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks weird 🙄 Is this necessary because of some quirk in the rules?
The only this might do is defer the call to
toString
on whatever typegetMessage()
returns. If that is already string, we don't gain anything from this...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding was that the parametrization is what avoids the call in case log level is not debug, so with
logger.debug(current.getMessage())
the call togetMessage()
is done even if log level is "info"while with
logger.debug("{}", current.getMessage())
the call togetMessage()
is only done if log level is "debug" but not if it's "info"Is my understanding incorrect?
Admittedly, in this particular case the call might not be particularly impactful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it indeed is. The only thing that is defered is the final call to
toString
I think.Maybe this gets clearer if we write this part of the code like this:
The parameterization can only help as far as Java itself allows it. Here, we are passing the value of
current.getMessage()
to the method, that is, we evaluate that part already when calling it.To make it a truly lazy evaluation we'd need a supplier, either for the whole message or for the inidividual parameters (see #5173 (comment)). I don't think the following code works, but that's how it would look like:
Here, we pass a function that, if evaluated, will make the lookup on how to get the message from
current
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does our logging framework allow for passing a
Supplier<String>
for the log message? Here, it would be nice to avoid the computation ofgetPlayerNameFromCharacter(character)
until we actually have to log it. Maybe that's guarded sufficiently, though, and INFO logs are enabled by default currently 🤷There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding was that the parametrization does exactly that - avoid the computation until we actually have to log it. But I don't know for sure, so I could be misunderstanding how this works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is so @jdrueckert :
https://dennis-xlc.gitbooks.io/the-logback-manual/content/en/chapter-2-architecture/logger-appenders-and-layouts/parameterized-logging.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@soloturn the parts mentioned in that manual I understood. It doesn't speak to method calls in the parameters, though.
But @skaldarnar's comment above explains it nicely 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ha, with you guys one learns something new all the time. there is indeed a fluent API to slf4j-2.0, @skaldarnar , which allows to pass suppliers. we are at slf4j-1.7 at the moment:
https://www.slf4j.org/manual.html#fluent