-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
Addresses the remaining findings that were not part of #5170 |
engine/src/main/java/org/terasology/engine/config/RenderingDebugConfig.java
Outdated
Show resolved
Hide resolved
engine/src/main/java/org/terasology/engine/core/TerasologyEngine.java
Outdated
Show resolved
Hide resolved
@@ -234,7 +234,7 @@ private void popStep() { | |||
current = null; | |||
if (!loadProcesses.isEmpty()) { | |||
current = loadProcesses.remove(); | |||
logger.debug(current.getMessage()); | |||
logger.debug("{}", current.getMessage()); |
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 type getMessage()
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 to getMessage()
is done even if log level is "info"
while with
logger.debug("{}", current.getMessage())
the call to getMessage()
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.
Is my understanding incorrect?
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:
// executed regardless of the log-level
Message message = current.getMessage();
// message.toString() is only called if DEBUG logs are enabled (happens inside)
logger.debug("{}", message);
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:
logger.debug(() -> current.getMessage());
Here, we pass a function that, if evaluated, will make the lookup on how to get the message from current
.
logger.info("Denied activation attempt by {} since the used entity does not exist on the authority", | ||
getPlayerNameFromCharacter(character)); |
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 of getPlayerNameFromCharacter(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.
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
@@ -183,7 +183,7 @@ public List<RenderPipelineTask> generateFrom(List<Node> orderedNodes) { | |||
|
|||
private void logList(List<?> list) { | |||
for (Object object : list) { | |||
logger.debug(object.toString()); | |||
logger.debug("{}", object.toString()); |
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.
see above @skaldarnar "weird" comment.
…valuation) (#5174) permit use of slf4j fluent api: https://www.slf4j.org/manual.html#fluent tried to upgrade to logback 1.3.x or logback 1.4.x, but code contains code depending on ch.qos.logback.core.joran.spi.InterpretationContext: engine/src/main/java/org/terasology/logback/RegexFilterAction.java for engine tests though, an upgrade was necessary as the code uses logback-classic directly.
engine/src/main/java/org/terasology/engine/rendering/dag/RenderTaskListGenerator.java
Outdated
Show resolved
Hide resolved
engine/src/main/java/org/terasology/engine/rendering/dag/RenderTaskListGenerator.java
Outdated
Show resolved
Hide resolved
engine/src/main/java/org/terasology/engine/rendering/nui/editor/layers/NUIEditorScreen.java
Outdated
Show resolved
Hide resolved
engine/src/main/java/org/terasology/engine/rendering/nui/editor/layers/NUISkinEditorScreen.java
Outdated
Show resolved
Hide resolved
Co-authored-by: Tobias Nett <[email protected]>
because("runtime: to configure logging during tests with logback.xml") | ||
} | ||
testRuntimeOnly("org.codehaus.janino:janino:3.1.7") { | ||
because("allows use of EvaluatorFilter in logback.xml") | ||
} | ||
testRuntimeOnly("org.slf4j:jul-to-slf4j:1.7.36") { | ||
testRuntimeOnly("org.slf4j:jul-to-slf4j:2.0.9") { |
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 that major version upgrade just work?
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.
looked like it, yes
This reverts commit c56c4dd.
* Revert "revert: "qa: parametrize logger usages (#5173)" (#5180)" This reverts commit 4f42a8f. * revert upgrade slf4j-2.0 * micrometer 1.9.8 --> 1.9.12 * qa, still use old slf4j, feedback jdrueckert. gradle does not care about the logger definition in engine-tests, it uses the one from engine also for the tests. remove it to not confuse. --------- Co-authored-by: soloturn <[email protected]>
PMD Rule Docs: https://docs.pmd-code.org/pmd-doc-6.55.0/pmd_rules_java_bestpractices.html#guardlogstatement
Theoretically fixes rule findings by using substituting parameters (parametrized logs).
Practically, false-positive findings still show up due to PMD bug: pmd/pmd#4703