【发布时间】:2015-08-25 09:49:52
【问题描述】:
我想将我的 maven 项目中的所有异常(显示在控制台中)记录到一个文件中。我创建了一个实现 UncaughtExceptionHandler 的类并调用 log4j Logger.error。这适用于主 maven 项目(Main 类在哪里),但不适用于连接的其他 maven 项目(所有 pom.xml 中的依赖项)。也许默认的 UncaughtExceptionHandler 被 Maven 覆盖了,但是我怎样才能让它工作呢?
public static void main(String[] args) throws Exception{
Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler());
}
public class DefaultExceptionHandler implements UncaughtExceptionHandler{
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultExceptionHandler.class);
@Override
public void uncaughtException(Thread thread, Throwable ex) {
LOGGER.error("Exception thread: "+thread.getName(), ex);
}
}
【问题讨论】:
标签: java maven exception log4j