Java Reference
In-Depth Information
ger.isDebugEnabled() before writing out debugging log messages. The logging
level can be changed from the logging configuration file.
9-11. Logging Exceptions
From the previous recipes you learned how to catch exceptions and how to log inform-
ation. This recipe will put these two recipes together.
Problem
You want to record exceptions in your log file.
Solution
Configure your application to use SLF4J. Utilize try / catch blocks to log exceptions
within the error log. In the following example, an SLF4J Logger is used to log mes-
sages from within an exception handler.
static Logger rootLogger = LoggerFactory.getLogger("");
private void start() {
loadLoggingConfiguration();
Thread.setDefaultUncaughtExceptionHandler((Thread t,
Throwable e) -> {
rootLogger.error("Error in thread "+t+" caused by
",e);
});
int c = 20/0;
}
private void loadLoggingConfiguration() {
FileInputStream ins = null;
try {
ins = new FileInputStream(new
File("logging.properties"));
LogManager.getLogManager().readConfiguration(ins);
Search WWH ::




Custom Search