Skip to content
Advertisement

logback don’t log exception into file which throwed from an ThreadPoolTaskExecutor pool thread

I’ve got an wired problem, that seems like the logback only print my Exception stack trace on the console,rather than log it into an log file. The following is my experiment codes, I used an spring boot test with two thread pool,which simulating my production codes.Both thread print a log and throws an RuntimeException.But the exception info only print on the console, rather than log into an file. Here is my code:

JavaScript

My ThreadPoolTaskExecutor configuration:

JavaScript

the log on the console:

JavaScript

The log in the file (only two info log):

JavaScript

And my logback configuration :

JavaScript

Advertisement

Answer

I was inspired by this question : Log runtime Exceptions in Java using log4j In JDK Thread class, there is a field called UncaughtExceptionHandler;

JavaScript

All I need is to override the ThreadGroup method, to set an UncaughtExceptionHandler in every Thread of my pool, and then my ThreadPoolTaskExecutor configuration look like:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement