-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make logger.error(ex);
, logger.info(ex);
, etc. print stack trace by default
#3433
Comments
@rgoers @ppkarwasz WDYT about this proposal? Instead of adding one more method to the |
First, I have to ask some obvious questions.
If I saw that I would tell them to add some descriptive text to describe what was being attempted. A stack trace with no other info is a software engineers nightmare. to be honest, I would very close to doing:
and then ignoring the event. |
Some thoughts:
Given Ralph's remarks, we should probably have an Error Prone rule that issues a warning if a |
Short answer: Because it's not the same thing.
Assuming someone only wrote this:
Yes, it's kidna weird that they don't add any additional context, but I guess it works for them ¯\(ツ)/¯. Maybe this is the only place that this specific exception is thrown/caught anyways. Maybe the exception message already contains all the additional context they need. I don't want to dictate how other people debug their own code, I primarily care about my own debugging experience.
That's unreasonable. |
Try: try {
doThing();
} catch (ThingFailedException ex) {
throw logger.throwing(new WhateverException(ex));
} Note: this is an anti-pattern (cc/@rgoers), because you don't trust the caller to properly handle the exception, so you log it for auditing purposes. If the caller handles the exception properly, it will appear twice in the logs, but if he ignores the exception or only shows it to the user (that later denies it) you can easily prove you didn't hide the error. I understand the intention, I see it often and sometimes I use it myself (should I trust the frontend dev ❓ ), but it is not a best practice. |
It is too easy to write code like this:
Especially if you come back to a project after months and you don't remember anything about the log4j API specifics.
You think you you log everything you need, but you don't get the exception's stack trace here. You'd have to write
logger.error("", ex);
here, which is extremely unintuitive! Orlogger.catching(ex)
, but that's another special thing I'd need to memorize.I propose to change the default behavior such that the stack trace is printed in this case. I don't care how it's achieved, e.g. by adding a
void error(Throwable)
overload, or adding a check in the existingvoid error(Object)
overload. The point is that it should be the default behavior because that's what's expected.The text was updated successfully, but these errors were encountered: