Log4j2 : change filename dynamically in RollingFile Appender

7.6k views Asked by At

I am using log4j2 and a rolling file appender, to roll file based on size and time. here is my config file and a dummy example:

log4j2.xml

    <RollingFile name="RollingFile" filename="log/${date:dd-MM-yyyy-HH}/currentRoll.log"   filePattern="log/%d{dd-MM-yyyy-HH}/Roll-%i.log">
        <PatternLayout>
            <Pattern>%d{ISO8601}{GMT} %p %c{1.c} [%t] %m%n</Pattern>
        </PatternLayout>

        <Policies>
            <TimeBasedTriggeringPolicy interval="1"/>
            <SizeBasedTriggeringPolicy size="3KB"/>
        </Policies>
    </RollingFile>

    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout>
            <Pattern>%d{ISO8601}{GMT} [%t] %-5p %c{1}: %m%n</Pattern>
        </PatternLayout>
    </Console>

</Appenders>

 <loggers>
    <Root level="info">
        <AppenderRef ref="Async"/>
    </Root>
 </loggers>
</Configuration>

and my dummy logging class:

public class Helloworld extends TimerTask {
private static final Logger logger=LogManager.getLogger("HelloWorld");


@Override
public void run(){
    logger.info("fake info message");
    logger.info("another fake info message");
 }
}

My problem is that my current log (currentRoll.log) file is in the folder that was first created when the program started. I would need it to be in the last. So for example if I start running the program at 1pm on November 25th, 2014 , and at 3pm I want to look at the current logs they would not be in the 25-11-2014-15 folder but in the 25-11-2014-13 folder.

My guess is that the ${date:dd-MM-yyyy-HH} is not resolved dynamically. I tried polling for automatic recofniguration using monitor interval:

<Configuration status="info" name="MyApp" packages="" monitorInterval="5">

and using the "$$" as in : $${date:dd-MM-yyyy-HH} but this gives me the following error:

"2014-11-26 17:22:27,143 ERROR Unable to create file log/${date:dd-MM-yyyy-HH-mm}/currentRoll.log java.io.IOException: The filename, directory name, or volume label syntax is incorrect

I know that log4net has a datePattern parameter that allows to resolve this problem, but I could not find an equivalent for log4j2.

Any suggestion on how to go about it?

1

There are 1 answers

4
AudioBubble On BEST ANSWER

The date used for the rolling appender is determined when it creates the rolled logfile. Or, in log4j terms, "the resolution of the whole RollingFile element is deferred until a match occurs". That is, the {date} property is interpolated at the specified resolution only when it goes to roll the file.

The $$ facility can be used for many properties, though I'm not sure if {date} is one of those. I have a feeling it is not, in which case the filespec being made will contain "${date...}" literally. This might be root of the error you are seeing.

I have tried, in the past, to use system properties via "$${sys:someProperty}" in appender file names (where you can set a default in the "Properties" section) but you have to make sure that you set the System property in question before you invoke the Logger at all, or you might end up with rolled files that only use the default value.

See the Log4J docs on property substitution for what I'm talking about.

At the end of the day, my own solution will probably require implementing the logger programmatically so I can change the root name of the log file, and abstract end-user-friendly properties into a simple property file instead of dumping an XML file on them.