How do you write a full stack trace to the log?

You can use

logger.log(Level.WARN, "logged exception", ex);

or

logger.warn("logged exception", ex);

Resources :

  • How to print the stack trace of an exception using Log4J (or Commons Logging)
  • logging.apache.org - Category

Usually:

log.warn("message", e);

But it depends on your logging framework too.


Using log4j this is done with:

logger.error("An error occurred", exception);

The first argument is a message to be displayed, the second is the exception (throwable) whose stacktrace is logged.

Another option is commons-logging, where it's the same:

log.error("Message", exception);

With java.util.logging this can be done via:

logger.log(Level.SEVERE, "Message", exception);