I want make log with throwable only when log-level is under debug mode.
My way is
try {
// ...
} catch (Exception e){
if(logger.isDebugEnabled()){
logger.warn("ERR Occurs", e);
} else {
logger.warn("ERR Occurs");
}
}
But it is hard to change all my code and It is difficult to see the code at a glance
Is there any alternatives?
There is a simpler one line equivalent to your own:
logger.warn("ERR Occurs", logger.isDebugEnabled() ? e : null);
You might consider packaging the ternary operator as a simple static method logEx(e)
if not wanting to override the logging class.
However: hiding exceptions is a bad idea - especially when not in debug mode, so you are better off with addressing the conditions causing the exception and leaving the logging as:
logger.warn("ERR Occurs", e);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.