我正在尝试获取log4j日志文件的文件名。我尝试了以下答案中给出的解决方案。
https://stackoverflow.com/a/4401387/1015678
Enumeration e = Logger.getRootLogger().getAllAppenders();
while ( e.hasMoreElements() ){
Appender app = (Appender)e.nextElement();
if ( app instanceof FileAppender ){
System.out.println("File: " + ((FileAppender)app).getFile());
}
}
但是,Logger.getRootLogger().getAllAppenders()
返回空枚举。 (我注意使用答案中提到的rootLogger
以下是我的log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<!-- This will be used if LOG_FILE_NAME env variable is not set -->
<Property name="LOG_FILE_NAME">my_app_log</Property>
</Properties>
<Appenders>
<RollingFile name="RollingFile-${web:contextPath}"
fileName="${sys:catalina.base}/logs/${env:LOG_FILE_NAME}.log" immediateFlush="true"
filePattern="${sys:catalina.base}/logs/${env:LOG_FILE_NAME}.log.%d{yyyy_MM_dd.HH_mm_ss}">
<PatternLayout pattern="%d{yyyyMMdd-HHmmss.SSS}|%-5p|%t| %-100m (%c{1})%n"/>
<Policies>
<OnStartupTriggeringPolicy/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="DEBUG" includeLocation="false">
<AppenderRef ref="RollingFile-${web:contextPath}"/>
</Root>
</Loggers>
</Configuration>
知道为什么吗?
答案 0 :(得分:4)
我刚刚对grep
apache-log4j-2.3-src
的所有来源getAllAppenders
进行了@SuppressWarnings("rawtypes")
public Enumeration getAllAppenders() {
return NullEnumeration.getInstance();
}
,我发现它只在一个地方实现了。
在Category.java中
/**
* Add an appender and see if it can be retrieved.
* Skipping this test as the Appender interface isn't compatible with legacy Log4j.
public void testAppender1() {
logger = Logger.getLogger("test");
a1 = new ListAppender("testAppender1");
logger.addAppender(a1);
Enumeration enumeration = logger.getAllAppenders();
Appender aHat = (Appender) enumeration.nextElement();
assertEquals(a1, aHat);
} */
/**
* Add an appender X, Y, remove X and check if Y is the only
* remaining appender.
* Skipping this test as the Appender interface isn't compatible with legacy Log4j.
public void testAppender2() {
a1 = new FileAppender();
a1.setName("testAppender2.1");
a2 = new FileAppender();
a2.setName("testAppender2.2");
logger = Logger.getLogger("test");
logger.addAppender(a1);
logger.addAppender(a2);
logger.removeAppender("testAppender2.1");
Enumeration enumeration = logger.getAllAppenders();
Appender aHat = (Appender) enumeration.nextElement();
assertEquals(a2, aHat);
assertTrue(!enumeration.hasMoreElements());
} */
上述方法返回其中没有元素的Enumeration。
另一个是在LoggerTest.java中,来执行测试用例。
Logger logger = LogManager.getLogger();
Map<String, Appender> appenderMap =
((org.apache.logging.log4j.core.Logger) logger).getAppenders();
看到上面的评论,看起来它已被弃用,甚至测试用例都被注释掉了。但我不确定,我试图找出他们是否曾在任何地方宣布,但无法找到。
所以我认为这会有所帮助,除非现在浪费时间进行调试。
正如本answer中所述,使用getAppenders api
var request = FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters: nil);
答案 1 :(得分:1)
您正在将Log4j 1构造与Log4j 2混合使用。您无法使用Log4j 1 API查询Log4j 2配置。
打印出FileAppender的文件名:
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
for (Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
if (entry.getValue() instanceof FileAppender) {
System.out.println("File: " + ((FileAppender)entry.getValue()).getFileName());
}
}