Log4J2 – assigning file appender filename at runtime

h/t rgoers
The FileAppender doesn’t support two dollar signs on the file name as the file is opened when the appender is started. What you are indicating with two dollar signs is that you want – potentially – a different file name for each event.

With a single $ (as in ${sys:logFilename}), the system will look for property “logFilename” in the system properties.

Thus, the log4j2.xml should have:

<appenders>
    <File name="MyFile" fileName="${sys:logFilename}">
        <PatternLayout pattern="%-4r %d{${datestamp}} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
</appenders>

The Java application should set the system property:

System.setProperty("logFilename", filename);

and reconfigure the logger:

org.apache.logging.log4j.core.LoggerContext ctx =
    (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
ctx.reconfigure();

This produces the desired behavior.

Leave a Comment