ThreadContext.pop()抛出NoSuchElementException:在log4j-2.3中ThreadContext堆栈为空

问题描述 投票:1回答:2

我正在尝试从log4j-1.x升级到log4j-2.3并在运行时收到波纹管错误。我错过了什么?

public void debug(String msg) {
    appendContext();
    logger.debug(msg);
    ThreadContext.pop();
}

[5/25/16 20:08:16:748 SGT] 0000006a ServletWrappe E com.ibm.ws.webcontainer.servlet.ServletWrapper服务SRVE0068E:应用程序中servlet [MWServlet]的一种服务方法抛出异常[DBIN中间件]。创建异常:[java.util.NoSuchElementException:在org.apache.logging.log4j.ThreadContext.pop(ThreadContext)的org.apache.logging.log4j.spi.DefaultThreadContextStack.pop(DefaultThreadContextStack.java:185)中,ThreadContext堆栈为空的.java:391)

java log4j2
2个回答
0
投票

我找到了解决方案,我们需要在调用pop()之前检查ThreadContext是否为空:

public void debug(String msg) {
    appendContext();
    logger.debug(msg);
    if(!ThreadContext.isEmpty()){
        ThreadContext.pop();
    }
}

0
投票

ThreadContext对象具有堆栈和映射对象。 ThreadContext.isEmpty()是一张地图上的支票,而ThreadContext.pop()正在堆叠。

以下检查对我有用:

   if (!ThreadContext.getImmutableStack().isEmpty()) {
        ThreadContext.pop();
    }
© www.soinside.com 2019 - 2024. All rights reserved.