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());
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());
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
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.
Your can also use:
logger.log(Level.INFO, "an exception was thrown", e);
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