How to add the date timestamp to log4j2 logfiles?

The pattern should not be given in the attribute "fileName" rather you have to specify the pattern in the attribute "filePattern" as like below.

<RollingFile name="RollingFile" fileName="${log-path}/filename.log" 
filePattern="${log-path}/filename-%d{yyyy-MM-dd}-%i.log" >
...
...
</RollingFile>

The "%i" is the counter that will be automatically incremented in rollover.

Hope this will help you.


To append the filename with date, replace %d with below format, i was having the same problem and but got by doing so :

<RollingFile name="APP" fileName="application-${date:yyyy-MM-dd}.log" />

Try this:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">

    <Properties>
        <Property name="log-path">D:/logs/</Property>
    </Properties>

    <Appenders>

        <RollingFile name="DebuggerLogger" fileName="${log-path}CMSAutomation.${date:yyyy-MM-dd_hh-mm-ss}.log" filePattern="${log-path}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
        </RollingFile>

    </Appenders>

    <Loggers>
        <Root level="ALL">
            <AppenderRef ref="DebuggerLogger"/>
        </Root>
    </Loggers>

</Configuration>

Tags:

Java

Log4J

Log4J2