18

My question is: it better to log with getMessage or with toString or both? taking in to account errors thrown by open source. Saw the questions in the comments but did not get an answer to this. Maybe I missed something ? Do not mind the small performance hit of logging one of them, but do not want to log both unless there is a good reason.

Meaning log(ex) or log(ex.getMessage), Not talking about stack trace.

Saw 1 , 2 and 3

Logging exceptions : which is better: log.warn(ex.getMessage(), ex) or log.warn(ex, ex);

I noticed sometimes getMessage returns empty or null, so in general practice is there any reason not to use :

log.warn(ex, ex);

As it seems to print the class name and the message (if set) ? I guess one reason could be if a sub class has over ridden to string not to print the message, but in reality do any of the hibernate, apache or spring libs do that?

Community
  • 1
  • 1
tgkprog
  • 4,493
  • 4
  • 41
  • 70
  • possible duplicate of [Should you report the message text of exceptions?](http://stackoverflow.com/questions/7320080/should-you-report-the-message-text-of-exceptions) – Raedwald Feb 02 '15 at 07:49
  • See also http://stackoverflow.com/questions/7361201/when-to-log-a-stacktrace-for-a-caught-exception – Raedwald Feb 02 '15 at 07:50
  • As the two linked questions show (which makes this question a duplicate), I think the answer is neither. Log a stack trace only for exceptions that indicate a bug; they are useful only for programmers; the presence of a bug is not something to merely warn about. Do not use the message text of exceptions in lig messages intended fir users (rather than orogrammers). – Raedwald Feb 02 '15 at 07:56
  • See also http://stackoverflow.com/questions/2031163/when-to-use-log-level-warn-vs-error – Raedwald Feb 02 '15 at 07:59
  • My question is it better to log with getMessage or with toString - taking in to account errors thrown by open source frameworks? Those other questions area about stack trace and not logging to the screen/ end user. – tgkprog Feb 02 '15 at 09:02
  • Hi @raedwald does my updated text make my question clearer? – tgkprog Feb 02 '15 at 09:06
  • 1
    Read through all the linked questions - they are definitely not duplicates... – Petr Bodnár Mar 27 '19 at 08:17

1 Answers1

21

How about

log.warn("some descriptive message, maybe with context {}", 
    someId, ex);

The exception details will already be printed as part of the stacktrace, so you don't need to include them in the message usually.

In case you want to suppress the stacktrace and only print the exception message, usually, ex.toString() works better than ex.getMessage(), because it also includes the exception class name, which the message does not. In fact, often the message is empty (for example with NullPointerExceptions).

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • Yes already do this, print local vars etc and use AOP for method params, was more about the exception itself toStirng or getMessage or both? Updated question – tgkprog Feb 02 '15 at 09:06
  • 1
    If you want to rephrase your question to `log.warn(ex);` vs `log.warn(ex.getMessage())` (with those the only two options), then yes: I vote for the first one. This includes the message, as specified by http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html#toString%28%29 Could someone override it to make it less useful? I guess. But I've never seen that happen. – Thilo Feb 02 '15 at 09:07
  • can you edit your answer just to talk about exception getMessage and toString ? – tgkprog Feb 14 '15 at 14:05
  • 1
    The answer is really partly a bit misleading, as it is generally quite a common mistake to expect that logging just the stacktrace after the logged line is enough. Because in many situations, you need to have the exception details on the first logged line already, minimally for being able to efficiently lookup an error, e. g. when searching the logged line by an exception name with a tool like `grep`. – Petr Bodnár Mar 27 '19 at 08:28