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

qa: parametrize logger usages #5173

Merged
merged 14 commits into from
Nov 19, 2023
Merged

qa: parametrize logger usages #5173

merged 14 commits into from
Nov 19, 2023

Conversation

jdrueckert
Copy link
Member

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

@jdrueckert jdrueckert added Type: Improvement Request for or addition/enhancement of a feature Topic: Stabilization Requests, Issues and Changes related to improving stablity and reducing flakyness Category: Test/QA Requests, Issues and Changes targeting tests and quality assurance labels Nov 14, 2023
@jdrueckert jdrueckert added this to the 2023 Revive - Milestone 2 milestone Nov 14, 2023
@jdrueckert jdrueckert self-assigned this Nov 14, 2023
@jdrueckert
Copy link
Member Author

Addresses the remaining findings that were not part of #5170

skaldarnar
skaldarnar previously approved these changes Nov 14, 2023
@@ -234,7 +234,7 @@ private void popStep() {
current = null;
if (!loadProcesses.isEmpty()) {
current = loadProcesses.remove();
logger.debug(current.getMessage());
logger.debug("{}", current.getMessage());
Copy link
Member

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...

Copy link
Member Author

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.

Copy link
Member

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.

Comment on lines +325 to +326
logger.info("Denied activation attempt by {} since the used entity does not exist on the authority",
getPlayerNameFromCharacter(character));
Copy link
Member

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 🤷

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

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 👍

Copy link
Contributor

@soloturn soloturn Nov 15, 2023

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());
Copy link
Contributor

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.
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") {
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

looked like it, yes

@jdrueckert jdrueckert merged commit c56c4dd into develop Nov 19, 2023
9 checks passed
@jdrueckert jdrueckert deleted the qa/logger branch November 19, 2023 20:48
jdrueckert added a commit that referenced this pull request Nov 19, 2023
jdrueckert added a commit that referenced this pull request Nov 19, 2023
@jdrueckert jdrueckert restored the qa/logger branch November 19, 2023 21:16
jdrueckert added a commit that referenced this pull request Nov 19, 2023
soloturn pushed a commit that referenced this pull request Nov 25, 2023
soloturn pushed a commit that referenced this pull request Dec 3, 2023
@jdrueckert jdrueckert deleted the qa/logger branch December 5, 2023 16:45
BenjaminAmos pushed a commit that referenced this pull request Dec 6, 2023
* 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 <soloturn@gmail.com>
soloturn pushed a commit that referenced this pull request Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Test/QA Requests, Issues and Changes targeting tests and quality assurance Topic: Stabilization Requests, Issues and Changes related to improving stablity and reducing flakyness Type: Improvement Request for or addition/enhancement of a feature
Projects
Status: Done
Status: No status
Status: No status
Development

Successfully merging this pull request may close these issues.

None yet

3 participants