Supersmuu
Supersmuu

Reputation: 3

log4j2: Using filename with digits in RollingFile appender

I have a log4j2 configuration, that just works fine.
But I want even the latest logfile to also have digits in the file name, so that the latest file is always on top.

Instead of this:

|-- Project
      |-- logfile.01.log
      |-- logfile.02.log
      |-- logfile.log

I want to look it like this:

|-- Project
      |-- logfile.01.log
      |-- logfile.02.log
      |-- logfile.03.log

Following is my configuration for logging with log4j2

log4j2.xml

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Configuration monitorInterval="60">
    <Properties>
        <Property name="pattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
        <Property name="filename" value="C:\Project\log\logfile.log"/>
        <Property name="filepattern" value="C:\Project\log\logfile.%02i.log"/>
        <Property name="filesize" value="10 MB"/>
        <Property name="rollovermax" value="40"/>
    </Properties>

    <Appenders>
        <RollingFile name="RollingFile" fileName="${filename}" filePattern="${filepattern}">
            <PatternLayout pattern="${pattern}"/>
            <Policies>
                <OnStartupTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="${filesize}"/>
            </Policies>
            <DefaultRolloverStrategy max="${rollovermax}" fileIndex="min"/>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

I tried to modify the filename in the following ways

<Property name="filename" value="C:\Project\log\logfile.01.log"/>
-> leads to only one logfile that will always be overwritten.
<Property name="filename" value="C:\Project\log\logfile.%02i.log"/>
-> leads to a logfile with %02i in it's name and then the normal rotation starts.
<Property name="filename" value="C:\Project\log\logfile.%i.log"/>
-> the same but with %i in it's name.

I even left the whole filename blank and empty but that only leads to no file at all.
<RollingFile name="RollingFile" fileName="" filePattern="${filepattern}">
<RollingFile name="RollingFile" fileName= filePattern="${filepattern}">

Upvotes: 0

Views: 45

Answers (0)

Related Questions