Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use current date pattern in log4j2 fileName?

<?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.

like image 627
membersound Avatar asked Dec 08 '22 23:12

membersound


2 Answers

Remove the filename attribute. It worked for me. (got the solution from: https://issues.apache.org/jira/browse/LOG4J2-1859) 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>
like image 61
Ravinda Dissanayake Avatar answered Jan 01 '23 04:01

Ravinda Dissanayake


This one worked (whyever):

<property name="filePattern">${date:yyyy-MM-dd}</property>
like image 40
membersound Avatar answered Jan 01 '23 05:01

membersound