How to use current date pattern in log4j2 fileName?
Asked Answered
G

4

6
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Properties>
        <property name="filePattern">%d{yyyy-MM-dd}</property>
    </Properties>
    <Appenders>
        <RollingFile name        ="TEST"
                     fileName    ="application-${filePattern}.log"
                     filePattern ="application-${filePattern}-rolled.log">
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>
    ...
</Configuration>

I'd like to use the current date directly in the written logfile. But the result of the configuration above is application-%{yyyy-MM-dd} as filename.

Why is the date placeholder not resolved? By the way: the renamed file on midnight is properly renamed like application-2016-03-13-rolled.log. Why does it work there, but not in the current logfile?

I'm running tomcat 8 and java 8, if that matters.

Gerome answered 14/3, 2016 at 8:17 Comment(0)
V
11

Remove the filename attribute. It worked for me.

(I got the solution from: https://issues.apache.org/jira/browse/LOG4J2-1859 Add rolling date support to fileName, RollingRandomAccessFile)

Here is my working configuration:

<RollingFile name           ="File"
             filePattern    ="${basePath}/api_test_execution_log_%d{yyyy-MM-dd}_%d{HH-mm-ss}_%i.log"
             immediateFlush ="true"
    >
    <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    <Policies>
        <TimeBasedTriggeringPolicy interval="1"
                                   modulate="true"/>
        <SizeBasedTriggeringPolicy size="32 MB"/>
        <OnStartupTriggeringPolicy/>
    </Policies>
</RollingFile>
Valonia answered 12/2, 2019 at 10:5 Comment(1)
can I combine this with keep max 15 files or so? DefaultRolloverStrategy caused :: RollingFileAppender 'MyAppender': When no file name is provided a DirectFileRolloverStrategy must be configuredPentode
G
6

This one worked (whyever):

<property name="filePattern">${date:yyyy-MM-dd}</property>
Gerome answered 14/3, 2016 at 8:24 Comment(1)
Yes, that worked fine for me: I had to define my appender this way: <RollingFile name="DailyRollingHTML" append="true" fileName="application-${filePattern}.log" filePattern="application-%d{yyyy-MM-dd}.log">Gilbertson
O
2

I don't know why the placeholder isn't resolved but here is my working configuration:

<Appenders>
    <RollingFile name           ="Permament"
                 fileName       ="E:/workspace/myproject/logs/ergo.log"
                 filePattern    ="E:/workspace/myproject/logs/ergo.%d{yyyy-MM-dd.HH:mm}.log"
                 immediateFlush ="true">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} %5p{length=5} - %c{1} %m %ex%n"/>
        <Policies>
            <TimeBasedTriggeringPolicy/>
        </Policies>
    </RollingFile>
    ...
Ouzel answered 14/3, 2016 at 8:22 Comment(0)
B
1

For those are using Log4j2 version 2.6.2 and below:

The fileName is indeed a file name that never changes while the application is running. If you do not want this behavior upgrade to the latest version of Log4j 2 and do not specify the fileName attribute.

From [LOG4J2-1859] Add rolling date support to fileName, RollingRandomAccessFile

Butlery answered 11/8, 2020 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.