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

Message formatting with last argument being Throwable #390

Open
zorba128 opened this issue Jan 25, 2024 · 0 comments
Open

Message formatting with last argument being Throwable #390

zorba128 opened this issue Jan 25, 2024 · 0 comments

Comments

@zorba128
Copy link

zorba128 commented Jan 25, 2024

I really often hit issue with slf4j interpreting my messages not the way I'd like it to.
I don't think its reasonable to have two following lines result in different behaviour.

Throwable ex = ...
Item item = ...

log.warn("Processing issue; ex={}, item={}", ex, item);   // this works fine
log.warn("Processing issue; item={}, ex={}", item, ex);   // and this produces "... issue=Issue(123), ex={}"  followed by stacktrace

I think the MessageFormatter should take into account number of placeholders, and try to use last argument as throwable candidate only if not bound.

FAQ says:

If the exception is not the last argument, it will be treated as a plain object and its stack trace will NOT be printed. However, such situations should not occur in practice.

For me this is not true. I quite often want to see just ex.toString, specially where exception is part of business logic. I just want to log its name, description, details - I don't care about stack trace. So it's common to treat exception just as any other part of message.

SLF4J, in accordance with the programmer's most probable intention, will interpret NumberFormatException instance as a throwable instead of an unused Object parameter

And this one actually suggests what should be done. It says about choice between exception and unused object parameter. And code actually does not check if parameter is used - it just picks last argument, resulting in message with last '{}' not filled in, and with unwanted stacktrace appended.

zorba128 pushed a commit to zorba128/slf4j that referenced this issue Jan 27, 2024
qos-ch#390
Signed-off-by: Marcin Kielar <marcin.kielar@superfund-technologies.com>
zorba128 pushed a commit to zorba128/slf4j that referenced this issue Feb 6, 2024
qos-ch#390
Signed-off-by: Marcin Kielar <zorba128@interia.pl>
zorba128 added a commit to zorba128/slf4j that referenced this issue Feb 6, 2024
qos-ch#390

Signed-off-by: Marcin Kielar <zorba128@interia.pl>
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

No branches or pull requests

1 participant