gpt4 book ai didi

java - log4j2 中异步记录器的行为

转载 作者:行者123 更新时间:2023-11-30 02:35:59 24 4
gpt4 key购买 nike

我的 log4j2.xml 包含同步和异步记录器。但是,当我使用异步记录器时,我只能打印第一个(共 5 个)log.debug 语句。

更新 3/28 --如果我介绍一个Thread.sleep(1)在 log.debug 调用之前..然后我能够获取通过 Async logger --> Rewrite Appender --> Rolling File Appender 记录的所有调试消息但不知道如何在没有 sleep 声明的情况下实现这一目标..

<Configuration status="WARN" packages="com.loggy.test">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>

<!-- Custom Appender Approach below -->
<Stub name="myapp" fileName="/Users/loggy/logs/myapp.log"
filePattern="logs/myapp-%d{MM-dd-yyyy}.log.gz">
<RegexFilter regex=".* special_log .*" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</Stub>
<!-- Custom Appender Approach above ends here -->

<RollingFile name="RollingFile" fileName="/Users/loggy/logs/roll_file_app.log"
filePattern="logs/app-%d{MM-dd-yyyy}.log.gz">
<PatternLayout>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>


<Rewrite name="Rewrite" ignoreExceptions = "false">
<CookieAppenderPolicy cookieNeeded="true">
</CookieAppenderPolicy>
<AppenderRef ref="RollingFile"/>
</Rewrite>
</Appenders>
<Loggers>
<AsyncLogger name="com.loggy.test" level="debug" includeLocation="true">
<AppenderRef ref="Rewrite" />
</AsyncLogger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>




package com.loggy.test;

public class TestJ {
@Inject
public static void main(String[] args)
{
final Logger log4j = LogManager.getLogger(TestJ.class
.getName());


log4j.trace(" special_log Trace");

/* When Async logger is used, Only this line below gets printed to the Rewrite Appender and hence to the Rolling File appender */

log4j.debug(" special_log debug published!");

log4j.info(" special_log test info");


log4j.debug(" sd_log test info");

/* Following lines NEVER gets published to the rolling file appender or to the Rewrite Appender */

log4j.debug(" special_log debug 2");

log4j.debug(" special_log debug 3");

log4j.debug(" special_log debug 4");

log4j.debug(" special_log debug 5");

}

但是,如果我包含 <AppenderRef ref="Rewrite" /><AsyncLogger>然后我就发布了所有的行。

关于应该对 log4j2.xml 进行哪些更改,以便在使用 AsyncLogger 时获得所有适当的行,有什么想法吗?

我还注意到 log4j2 包内有 AsyncLoggerConfig.java类代码永远不会到达 super.callAppenders(event);

我假设 super.callAppenders(event)必须为要调用的引用的附加程序工作吗?如何使这一行通过 log4j2.xml 执行?

/**  AsyncLoggerConfig.java
* Passes on the event to a separate thread that will call
* {@link #asyncCallAppenders(LogEvent)}.
*/
@Override
protected void callAppenders(final LogEvent event) {
// populate lazily initialized fields
event.getSource();

event.getThreadName();

// pass on the event to a separate thread
if (!helper.callAppendersFromAnotherThread(event)) {
super.callAppenders(event);
}
}
<小时/>

最佳答案

此问题已在 Log4j 的更高版本中得到解决。解决方案是从版本 2.0-rc1 升级到最新版本(撰写本文时为 2.8.1)。

关于java - log4j2 中异步记录器的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43059568/

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