Updating log4j2 appender fileName gives ERROR FileManager java.io.IOException: The filename, directory name, or volume label syntax is incorrect

I have a log4j2.xml file that I use to define some loggers that output to .log files.

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{MM-dd HH:mm:ss  } %p - %m%n"/>
        </Console>

        <File name="LogFileAppender" fileName="${sys:logFileName}" append="false">
            <PatternLayout>
                <Pattern>%d [%t] %p - %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>

    <Loggers>
        <Root level="error">
            <AppenderRef ref="ConsoleAppender" />
            <AppenderRef ref="LogFileAppender"/>

        </Root>
    </Loggers>
</Configuration>

I set the full paths for the logger fileNames in java after the .xml is already read in.

public static void initializeLoggers(String logPath)
{
    // set output paths for log files
    System.setProperty("logFileName", logPath + "PROJECT.log");

    // reconfigure loggers with paths
    org.apache.logging.log4j.core.LoggerContext ctx =
            (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
    ctx.reconfigure();
}

The code works as expected and logs are output to the updated path, but I am running into these errors below:

2022-01-18 15:05:26,685 main ERROR FileManager (${sys:logFileName}) java.io.IOException: The filename, directory name, or volume label syntax is incorrect java.io.IOException: The filename, directory name, or volume label syntax is incorrect

2022-01-18 15:05:26,693 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.FileAppender for element File: java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory@4d826d77] unable to create manager for [${sys:logFileName}] with data [org.apache.logging.log4j.core.appender.FileManager$FactoryData@61009542] java.lang.IllegalStateException: ManagerFactory [org.apache.logging.log4j.core.appender.FileManager$FileManagerFactory@4d826d77] unable to create manager for [${sys:logFileName}] with data [org.apache.logging.log4j.core.appender.FileManager$FactoryData@61009542]

2022-01-18 15:05:26,699 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.FileAppender for element File: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.FileAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.FileAppender

This is strange since the path I update it to is correct and the resulting logs are output in the desired directory along with all the information inside. No logging happens before initializeLoggers() as well. Any help would be appreciated! I referenced this post for the properties updating solution


Solution 1:

The code was calling LogManager.getLoggers() before the loggers were initialized with a filename value. I just set the default value like so and the code runs without error now:

<File name="LogFileAppender" fileName="${sys:logFileName:-default}" append="false">
    <PatternLayout>
        <Pattern>%d [%t] %p - %m%n</Pattern>
    </PatternLayout>
</File>