I am using the log4j2 version 2.11.1. I have the below pattern in log4j2.xml
"%d %-5r %-5p [%t] [%x] [%c] [%M] [%L] - %m%n". I want to filter the logs on [%t]
I tried RegexFilter=".Coherence." and ThreadContextMapFilter. Filter is not working.
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config" status="DEBUG"> <Properties> <Property name="basePath">../Log4j2Example/logs</Property> </Properties> <Appenders> <!-- File Appender --> <File name="FILE" fileName="${basePath}/logfile.log" append="true" > <PatternLayout pattern="%d %-5r %-5p [%t] [%x] [%c] [%M] [%L] - %m%n" /> <RegexFilter regex=".*Cohernce.*" useRawMsg="true" onMatch="DENY" onMismatch="ACCEPT"/> </File> <File name="STRINGFILE" fileName="${basePath}/logfileAA.log" append="true" > <PatternLayout pattern="%d %-5r %-5p [%t] [%x] [%c] [%M] [%L] - %m%n" /> <RegexFilter regex=".*Cohernce.*" onMatch="ACCEPT" useRawMsg="true" onMismatch="DENY"/> </File> </Appenders> <Loggers> <Logger name="com.jcg.logging" level="debug" additivity="false"> <AppenderRef ref="FILE" /> <AppenderRef ref="STRINGFILE" /> </Logger> <Logger name="com.jcg.logging" level="debug" additivity="false"> <AppenderRef ref="FILEA"/> <AppenderRef ref="STRINGFILE" /> </Logger> <Root level="DEBUG"> <AppenderRef ref="STDOUT" /> </Root> </Loggers> </Configuration>
2019-06-12 10:41:06,262 486977 DEBUG [Coherence:BaseCacheService2Worker:0] [[]] [com.test.logging.Aggregator] [aggregate] [46]-内部聚合方法-START
2019-06-12 10:41:06,263 486979调试[来源:Coherence:BaseCacheService2Worker] [[]] [com.test.abc.xyz] [XXX] [246]-内部方法-START
任何包含一致性的行都应重定向到log4j2。Appenders“ STRINGFILE”和“ FILE” Appender不记录它。
答案 0 :(得分:0)
您可以使用RoutingAppender的getThreadName和LogEvent方法执行所需的操作。
这是一个简单的Java类,用于生成一些日志记录:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class SomeClass {
private static final Logger log = LogManager.getLogger();
public static void main(String[] args){
log.debug("This is some debug!");
log.info("Here's some info!");
log.error("Some error happened!");
Thread t = new Thread(new Runnable(){
@Override
public void run() {
log.info("Here's some info!");
log.error("Some error happened!");
}
}, "foo.Cohernce.1234");
t.start();
try {
t.join();
} catch (InterruptedException e) {
log.error(e);
}
}
}
这是log4j2.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<File name="FILE" fileName="logs/logfile.log" append="true" >
<PatternLayout pattern="%d %-5r %-5p [%t] [%x] [%c] [%M] [%L] - %m%n" />
</File>
<File name="STRINGFILE" fileName="logs/logfileAA.log" append="true" >
<PatternLayout pattern="%d %-5r %-5p [%t] [%x] [%c] [%M] [%L] - %m%n" />
</File>
<Routing name="Routing">
<Routes>
<Script name="RoutingInit" language="JavaScript"><![CDATA[
if (logEvent.getThreadName() != null && logEvent.getThreadName().contains("Cohernce")) {
"Cohernce";
}else{
"OTHER";
}]]>
</Script>
<Route ref="FILE" />
<Route ref="STRINGFILE" key="Cohernce" />
</Routes>
</Routing>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Routing" />
</Root>
</Loggers>
</Configuration>
这是写入logfile.log的输出
2019-06-15 01:30:00,705 645 DEBUG [main] [[]] [example.SomeClass] [main] [12] - This is some debug!
2019-06-15 01:30:00,815 755 INFO [main] [[]] [example.SomeClass] [main] [13] - Here's some info!
2019-06-15 01:30:00,818 758 ERROR [main] [[]] [example.SomeClass] [main] [14] - Some error happened!
这是写入logfileAA.log的输出
2019-06-15 01:30:00,822 762 INFO [foo.Cohernce.1234] [[]] [example.SomeClass] [run] [21] - Here's some info!
2019-06-15 01:30:00,832 772 ERROR [foo.Cohernce.1234] [[]] [example.SomeClass] [run] [22] - Some error happened!