gpt4 book ai didi

logback - 如何以编程方式访问 logback-access 配置的附加程序?

转载 作者:行者123 更新时间:2023-12-02 00:22:24 29 4
gpt4 key购买 nike

我已经使用 LogbackValve 为 Tomcat 7.0 启用了 logback-access 日志。我的 logback-access 配置如下所示。

<configuration>  
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>access.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>access.%d{yyyy-MM-dd}.log.zip</fileNamePattern>
</rollingPolicy>

<encoder>
<pattern>combined</pattern>
</encoder>
</appender>

<appender-ref ref="FILE" />
</configuration>

现在一切正常,我可以看到访问日志。但在其他一些代码中,我想以编程方式检索 logback-access.xml 中配置的附加程序。有没有办法做到。由于没有定义 Logger,因此您不能使用 LoggerContext。

最佳答案

您可以使用 LogbackAccessContext 检索 logback-access.xml 中配置的附加程序。这是我的解决方案:

TomcatServletWebServerFactory sc = (TomcatServletWebServerFactory) context.getBean(TomcatServletWebServerFactory.class);
List<Valve> values = (List<Valve>) sc.getEngineValves();
LogbackAccessTomcatValve accessTomcatValve = null;
for(Valve value : values) {
if(value.getClass() == LogbackAccessTomcatValve.class) {
accessTomcatValve = (LogbackAccessTomcatValve) value;
}
}

//get logbackAccessContext from the logbackAccessTomcatValve by invoking.
LogbackAccessContext logbackAccessContext = null;
Field field = null;
try {
field = accessTomcatValve.getClass().getDeclaredField("logbackAccessContext");
field.setAccessible(true);
logbackAccessContext = (LogbackAccessContext) field.get(accessTomcatValve);
} catch (Exception e) {
log.error("Exception happened when fetching the logback access context ." +e.getMessage());
}

关于logback - 如何以编程方式访问 logback-access 配置的附加程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39967321/

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