-4

Which method is more preferred (As per good programming) for production to easily debug error.

LOG.error("Error message ",e);

OR

LOG.error("Error message "+e);

OR

LOG.error("Error message "+e.getMessage());
Pawan Kumar
  • 1,511
  • 4
  • 15
  • 18
  • 1
    Do you at least know the differences? – Tom Oct 07 '15 at 11:34
  • @Tom Why is this on hold as opinion-based? The first case gives the logging framework all information needed, and the logging framework then can decide what info to actually log. The later approaches only give to the logging framework the message, and make it impossible to manage what is logged centrally via logging configuration. – Jiri Tousek Oct 07 '15 at 13:39
  • You want to know which approach is the best, so everyone answer that with what he or she thinks is best. Therefore it is opinion-based. – Tom Oct 07 '15 at 13:48
  • @Pawan Stating which logging framework you're using would probably help. – Jiri Tousek Oct 07 '15 at 14:59
  • None of the above; [do not report the message text of exceptions unless they indicate bugs](http://stackoverflow.com/questions/7320080/should-you-report-the-message-text-of-exceptions). – Raedwald Mar 24 '16 at 16:19

4 Answers4

7

Why do you not just try it out? You are the only one who knows which information do you need in your logging. Depending on the thrown exception you might want the stacktrace or not.

See below a small example to show the different outputs

private void logTest() {
    try {
        Files.readAllBytes(Paths.get("foobar"));
    } catch (IOException e) {
        logger.error("Error message ", e);
        logger.error("Error message " + e);
        logger.error("Error message " + e.getMessage());
    }
}

logger.error("Error message ", e);

2015-10-07 13:42:11,239 [main] ERROR sub.optimal.Main  - Error message 
java.nio.file.NoSuchFileException: foobar
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
    at java.nio.file.Files.newByteChannel(Files.java:361)
    at java.nio.file.Files.newByteChannel(Files.java:407)
    at java.nio.file.Files.readAllBytes(Files.java:3152)
    at sub.optimal.Main.logTest(Main.java:43)
    at sub.optimal.Main.main(Main.java:53)

logger.error("Error message " + e);

2015-10-07 13:42:11,243 [main] ERROR sub.optimal.Main  - Error message java.nio.file.NoSuchFileException: foobar

logger.error("Error message " + e.getMessage());

2015-10-07 13:42:11,243 [main] ERROR sub.optimal.Main  - Error message foobar
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • Thanks @SubOptimal I tried it but i am confused that which one is good practice for production and effective development. – Pawan Kumar Oct 07 '15 at 11:57
  • As I said it depends. If you know the exception itself contains all information you need to know for sport the error, like `java.nio.file.NoSuchFileException: foobar` then you might not want to clutter the log with the stacktrace. If you catch an exception where you don't know what could be the cause or which information you might need to spot the origin, log as much as possible (means include the stacktrace). – SubOptimal Oct 07 '15 at 12:19
  • @SubOptimal why not leave the decision of what to log on the apender's layout? If you use error(String, Throwable), you can decide in the layout whether you want the message, the exception message, and the stack trace to be present in the log (and often you can change it even without recompiling). While if you use any of the other proposed forms, there's no way to configure the logger to print the stacktrace. – Jiri Tousek Oct 07 '15 at 13:27
  • @JiriTousek Dependent on the logger implementation this might be an option not all implementation might provide this feature (I'm not aware of this functionality in log4j before version 2). There might be a problem if different exception handlers in one class should log different level of exception information. – SubOptimal Oct 07 '15 at 14:13
3

Use LOG.error("Error message ",e); - that way you won't lose the stack trace of the exception (note that you also need to make sure your logging framework's output layout is set up so that it prints the stack traces).

Stack traces are a valuable part of the exception report since they tell you how the offending code was called, and therefore may help you understand what state your program was in when the error occurred.

If you for some reason want to log only the message and not the stack trace, I would still recommend using the above form and then configuring the logging framework to not print the stack trace (in Log4j, this is done by configuring the Appender). That way, if you change your mind later, you can add the stack trace to the logs by changing the configuration only, without the need to change the actual logging code. The choice of what info should be put in the log should be responsibility of the appender configuration, not the code that logs the entry.

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • [do not report the message text of exceptions unless they indicate bugs](http://stackoverflow.com/questions/7320080/should-you-report-the-message-text-of-exceptions). – Raedwald Mar 24 '16 at 16:20
  • @Raedwald I would however like to point out two important things: 1) "Show to user" and "print to log" are two very different things. While the user needs a clear, understandable message, in logs you want as much detail as practicable. Even the accepted answer of the linked question says that. 2) Even an exception that *possibly* indicates a bug is worth logging, not just those that are clear bugs. – Jiri Tousek Mar 29 '16 at 07:18
0

Your can also use:

logger.log(Level.INFO, "an exception was thrown", e);
-1

In Java

LOG.error("Error message ",e);

LOG.error("Error message "+e);

You are passing the exception object which will invoke the to String implementation

LOG.error("Error message "+e.getMessage());

This will give you the content of getMessage

Sagar
  • 15
  • 4
  • I agree to Sagar .. But its always good to have stack trace in case of exceptions . I suggest LOG.error(""Error message " + e. printStackTrace()); – Priyambada Madala Oct 07 '15 at 11:51
  • *"You are passing the exception object which will invoke the to String implementation"* This is wrong for the first code line. – Tom Oct 07 '15 at 12:56
  • @PriyambadaMadala If you want the stracktrace, then use `LOG.error("Error message ", e);`. Calling `printStackTrace` is not necessary. – Tom Oct 07 '15 at 12:58