0

I'm quite new to the Log4j2, so may be it isn't a bug just I don't know how to use it properly.

I would like to configure the Log4j2 via configuration file specified by the log4j.configurationFile system property. In my previous experiments I was able to configure the log4j2 with the properly placed log4j2.xml, and worked fine. Now if the system property exists I got 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

I have no clue. It seems everything fine to me. Here is my property file "src/main/resources/AppProperties.properties":

log4j.configurationFile=src/main/resources/log4j2_custom.xml

The relevant part of the 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>

The tests:

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);
    }

}

And finally here is the dependencies from the 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>

Please let me know if you found something. Any idea is greatly appreciated.

mTUX
  • 61
  • 1
  • 5
  • I can't imagine anyone would want to read through all of this - can you cut it down to a minimal set of information, and perhaps use a bit of highlighting to indicate where your specific question is? (I couldn't find a question mark except in the code). – JustinJDavies Aug 25 '14 at 15:20
  • Thank you for your answer. It is quite difficult what you ask, since I don't know where I made the mistake. My specific question is why I am getting NullPointerException, so the most relevant information is the first code, the stack trace. Anyway, I going to cut the post as much as I can. – mTUX Aug 26 '14 at 07:15
  • The problem already seems resolved, so this is unrelated to the question, but you will get better performance if you use %c (lowercase) in your pattern instead of %C. Also monitorIntervall="30" should be `monitorInterval` (with one "l" at the end). – Remko Popma Aug 26 '14 at 19:30

1 Answers1

1

I finally realized the problem is totally unrelated to the Log4j2. When I intended to add the property to the system properties I have accidentally cleared every system property except the log4j.configurationFile. That was the root cause of all the strange exception originated from the JDK.

mTUX
  • 61
  • 1
  • 5