Skip to content

fix: use logger name in default log configuration #21154

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jbee
Copy link
Contributor

@jbee jbee commented Jun 5, 2025

Summary

Using %F means "the class name of the class that called the logger method". I propose that the better and less confusing name to use is %c{1} which means the name of the logger (%c) as simple name ({1}), meaning if there is a package name in the logger name cut it out.

Confusion

This can be very confusing if meta-programming is used to resolve different loggers.
With %F the log line would state the name of the class containing the meta-logging code.
If someone would want to configure the log level based they are likely to mistakenly assume that the class name in the logs would be the target of the level change. But that is not correct. The target still needs to be selected based on the logger name.
Also using %F defies the purpose or usefulness of resolving different loggers because it does no longer matter which logger a log is written to, the name in the log entry will always be the one containing the code that calls the "logger.log" method.
That is why it is more useful and less confusing to use %c or %c{1}.

An example:
%F gives a line with TimeExecutionInterceptor.java

* INFO  2025-06-05T17:31:22,496 Completed min-max import in 190 ms (TimeExecutionInterceptor.java [http-nio-9090-exec-8]) IDmLJhitmkBNAux4LypF/5SH8VILsaLpyFNJNOGX24hCg= 

But the logger used here was actually MinMaxDataElementService. Changing the log level of TimeExecutionInterceptor would not have any noticeable impact because that class is not the logger class.

With %c{1} the same log line would be

* INFO  2025-06-05T17:31:22,496 Completed min-max import in 190 ms (MinMaxDataElementService.java [http-nio-9090-exec-8]) IDmLJhitmkBNAux4LypF/5SH8VILsaLpyFNJNOGX24hCg= 

Performance

Using %F is also discouraged since it has a larger performance overhead. This is because in order to find which class called the logger method the framework needs to throw a fake exception, catch it and look at the stack trace to find this out. This overhead does not exist with %c.

Documentation

The %F line is also given as an example in our documentation, we might want to change that as well?

https://docs.dhis2.org/en/manage/reference/logging.html?h=#log-configuration

Using `%F` means _"the class name of the class that called the logger method"_.  I propose that the better and less confusing name to use is `%c{1}` which means the name of the logger (`%c`) as simple name (`{1}`), meaning if there is a package name in the logger name cut it out.

This can be very confusing if meta-programming is used to resolve different loggers and log
@jbee jbee self-assigned this Jun 5, 2025
@jbee
Copy link
Contributor Author

jbee commented Jun 5, 2025

@stian-sandvold , @Philip-Larsen-Donnelly please have a look and make up your mind about if we want to move forward with this change. Thanks.

Copy link

sonarqubecloud bot commented Jun 5, 2025

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.

1 participant