gpt4 book ai didi

java - 用于 Log4j 2 的 Log4jNestedDiagnosticContextFilter

转载 作者:搜寻专家 更新时间:2023-11-01 03:17:14 25 4
gpt4 key购买 nike

从 Spring 4.2.1 开始,Log4jNestedDiagnosticContextFilter 在 Apache 的 log4j 1.x EOL 声明之后被弃用,如下所示。

enter image description here

我找到了相应的 JIRA Ticket ( SPR-13400 ),但如何将 Log4j 2 引入 Spring 环境以执行与 Log4jNestedDiagnosticContextFilter 相同的操作?

最佳答案

我自己实现了它,方法是复制 Log4jNestedDiagnosticContextFilter 并将记录器调整为 Log4j 2,并将 NDC 更改为 ThreadContext

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.springframework.web.filter.AbstractRequestLoggingFilter;

import javax.servlet.http.HttpServletRequest;

public class CustomRequestLoggingFilter extends AbstractRequestLoggingFilter {

protected final Logger log4jLogger = LogManager.getLogger(getClass());

@Override
protected void beforeRequest(HttpServletRequest request, String message) {
if (log4jLogger.isDebugEnabled()) {
log4jLogger.debug(message);
}
ThreadContext.push(getNestedDiagnosticContextMessage(request));
}

@Override
protected void afterRequest(HttpServletRequest request, String message) {
ThreadContext.pop();
if (ThreadContext.getDepth() == 0) {
ThreadContext.removeStack();
}
if (log4jLogger.isDebugEnabled()) {
log4jLogger.debug(message);
}
}

protected String getNestedDiagnosticContextMessage(HttpServletRequest request) {
return createMessage(request, "", "");
}
}

关于java - 用于 Log4j 2 的 Log4jNestedDiagnosticContextFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com