我对Log4j2很新,所以可能它不是一个bug,只是我不知道如何正确使用它。
我想通过log4j.configurationFile系统属性指定的配置文件配置Log4j2。 在我之前的实验中,我能够使用正确放置的log4j2.xml配置log4j2,并且工作正常。 现在,如果系统属性存在,我得到NullPointerException:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running newpackage.Log4jSyspropProblemTest
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Caused by: java.lang.NullPointerException
at java.io.Writer.write(Writer.java:157)
at java.io.BufferedWriter.newLine(BufferedWriter.java:243)
at java.io.PrintStream.newLine(PrintStream.java:544)
at java.io.PrintStream.println(PrintStream.java:824)
at java.lang.Throwable$WrappedPrintStream.println(Throwable.java:748)
at java.lang.Throwable.printStackTrace(Throwable.java:655)
at java.lang.Throwable.printStackTrace(Throwable.java:643)
at org.apache.logging.log4j.simple.SimpleLogger.logMessage(SimpleLogger.java:165)
at org.apache.logging.log4j.status.StatusLogger.logMessage(StatusLogger.java:220)
at org.apache.logging.log4j.spi.AbstractLogger.logMessage(AbstractLogger.java:1336)
at org.apache.logging.log4j.spi.AbstractLogger.logIfEnabled(AbstractLogger.java:1325)
at org.apache.logging.log4j.spi.AbstractLogger.error(AbstractLogger.java:551)
at org.apache.logging.log4j.core.pattern.PatternParser.createConverter(PatternParser.java:528)
at org.apache.logging.log4j.core.pattern.PatternParser.finalizeConverter(PatternParser.java:569)
at org.apache.logging.log4j.core.pattern.PatternParser.parse(PatternParser.java:364)
at org.apache.logging.log4j.core.pattern.PatternParser.parse(PatternParser.java:163)
at org.apache.logging.log4j.core.layout.PatternLayout.<init>(PatternLayout.java:124)
at org.apache.logging.log4j.core.layout.PatternLayout.<init>(PatternLayout.java:54)
at org.apache.logging.log4j.core.layout.PatternLayout$Builder.build(PatternLayout.java:368)
at org.apache.logging.log4j.core.config.DefaultConfiguration.<init>(DefaultConfiguration.java:56)
at org.apache.logging.log4j.core.LoggerContext.<init>(LoggerContext.java:72)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:218)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:144)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:80)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:72)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:468)
at newpackage.Log4jSyspropProblemTest.testLoggerSuccesfullyLoaded(Log4jSyspropProblemTest.java:44)
... 30 more
我不知道。对我来说似乎一切都很好。 这是我的属性文件“src / main / resources / AppProperties.properties”:
log4j.configurationFile=src/main/resources/log4j2_custom.xml
src / main / resources / log4j2_custom.xml的相关部分:
<configuration status="info" monitorIntervall="30" strict="true">
<properties>
<Property name="filename">logs/myLog.log</Property>
</properties>
<appenders>
<appender type="File" name="fileAppender" filename="${filename}">
<layout type="PatternLayout">
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
</layout>
</appender>
</appenders>
<loggers>
<logger name="fileLogger">
<appenderRef ref="fileAppender"/>
</logger>
</loggers>
</configuration>
测试:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4jSyspropProblemTest {
private Logger fileLogger;
private static final String PROPERTIES_FILE = "src/main/resources/AppProperties.properties";
private static final String LOG4J_CONFIGURATION_PROPERTY_NAME = "log4j.configurationFile";
@Test
public void testPropertyIsValid(){
final String configFile = System.getProperty(LOG4J_CONFIGURATION_PROPERTY_NAME);
Assert.assertNotNull(configFile);
Assert.assertTrue(new File(configFile).exists());
}
@Test
public void testLoggerSuccesfullyLoaded(){
fileLogger = LogManager.getLogger("fileLogger");
Assert.assertNotNull(fileLogger);
}
}
最后这里是pom.xml的依赖项:
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
如果你发现了什么,请告诉我。非常感谢任何想法。
答案 0 :(得分:1)
我终于意识到这个问题与Log4j2完全无关。当我打算将属性添加到系统属性时,我意外地清除了除log4j.configurationFile之外的每个系统属性。 这是源自JDK的所有奇怪异常的根本原因。