我有 log4j2 xml 配置,但我想将此配置转换为编程方式
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.x/config" packages="org.graylog2.log4j2">
<Appenders>
<GELF name="gelfAppender" server="graylog.XXX.com" port="12212" hostname="wskurumsaltest.XXX.com" protocol="UDP" includeThreadContext="true">
<KeyValuePair key="grayloggroup" value="TEST_GROUP" />
<KeyValuePair key="application" value="TEST" />
<KeyValuePair key="environment" value="TEST" />
<PatternLayout pattern="%X{applicationNames} - %m%n"/>
</GELF>
<RollingFile name="activityStatusFileAppender"
fileName="${filePath}/activityStatus.out"
<PatternLayout>
<Pattern>%d - %m%n</Pattern>
</PatternLayout>
</RollingFile>
</Appenders>
<Logger name="activityStatusFileLogger" level="debug">
<AppenderRef ref="activityStatusFileAppender" />
<AppenderRef ref="gelfAppender" />
</Logger>
</Loggers>
</Configuration>
我想将 gelFAppender 添加到“activityStatusFileLogger”中,因为编程方式不使用 xml
GelfAppender gelfAppender = new GelfAppender();
gelfAppender.setGraylogHost("graylog.xxx.com");
gelfAppender.setGraylogPort(12212);
gelfAppender.setOriginHost("graylog.xxx.com");
gelfAppender.setAdditionalFields("{\"grayloggroup\": \"group\", \"application\": \"test\", \"environment\": \"test\"}");
gelfAppender.activateOptions();//
Logger x= LogManager.getLogger("activityStatusFileLogger");
x.addAppender(gelfAppender);
但是 LogManager.getLogger() 不包含“addAppenders()”方法。LogManager 包含在 log4j-api-2.10.0 中。jar 和 org.apache.logging.log4j 包。