springboot 日志没有记录异常

背景

springboot项目,放到服务器上跑,定时任务运行过程中中断,查看日志却发现没有报错。
在本地跑,发现控制台能打印报错信息,而日志也没有记录报错。

经排查,发现是因为报错出现在线程池中,没有在日志中记录。
原先使用线程池:

ExecutorService executorService = Executors.newFixedThreadPool(15);

解决

新建类继承ThreadPoolExecutor,重写afterExecute方法。

@Slf4j
public class TaskExecutor extends ThreadPoolExecutor {
    public TaskExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        if (t != null) {
            log.error(t.getMessage(), t);
        }
    }
}

使用:

   ExecutorService executorService = new TaskExecutor(10, 15,
            0L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());

日志中就有异常信息了。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值