I'm currently using Log4J2 version 2.14.1 (as part of SpringBoot version 2.5.0)
What I Want to Do
I'm trying to add a RollingFile appender to my Log4J2 configuration which will:
- Roll-over on application startup.
- Roll-over when a maximum size has been reached (10 MB in the below example).
- Roll-over each day.
- Roll-over into indexed files with the date of their roll-over in the file-name and the parent folder-name.
- Only keep a finite number of archived/rolled-over files (3 in the below example).
What I'd Like the Log Folder(s) to Look Like
There would only be a finite number of archived log files kept across all days.
-- logs
|-- master-backend-log4j2.log
|-- 2021-06
|-- master-backend-log4j2-2021-06-30-1.log.gz
|-- 2021-07
|-- master-backend-log4j2-2021-07-01-1.log.gz
|-- master-backend-log4j2-2021-07-01-2.log.gz
What the Log Folder(s) Look Like
There is a finite number of archived log files kept per day. In my final configuration, the maximum number of files to keep would be much higher than 3, but this lower number makes the below examples easier :)
-- logs
|-- master-backend-log4j2.log
|-- 2021-06
|-- master-backend-log4j2-2021-06-29-1.log.gz
|-- master-backend-log4j2-2021-06-29-2.log.gz
|-- master-backend-log4j2-2021-06-29-3.log.gz
|-- master-backend-log4j2-2021-06-30-1.log.gz
|-- master-backend-log4j2-2021-06-30-2.log.gz
|-- master-backend-log4j2-2021-06-30-3.log.gz
|-- 2021-07
|-- master-backend-log4j2-2021-07-01-1.log.gz
|-- master-backend-log4j2-2021-07-01-2.log.gz
|-- master-backend-log4j2-2021-07-01-3.log.gz
My Log4J2 Configuration
<RollingFile
name="MasterRollingFile"
fileName="./logs/master-backend-log4j2.log"
filePattern="./logs/${date:yyyy-MM}/master-backend-log4j2-%d{yyyy-MM-dd}-%i.log.gz"
>
<PatternLayout
pattern="%d [%-5level] (%C) : %m%n%throwable"
/>
<Policies>
<!--
Rollover on startup, per day and when
the file reaches a maximum size.
-->
<OnStartupTriggeringPolicy
minSize="1"
/>
<SizeBasedTriggeringPolicy
size="10 MB"
/>
<TimeBasedTriggeringPolicy
interval="1"
modulate="true"
/>
</Policies>
<DefaultRolloverStrategy
max="3"
/>
</RollingFile>
Duplicate Question with Inadequate Answer
I found this post with basically the same issue, but the person asking the question answered it herself by stating that the solution is to remove the time restrin: How to change my log4j2.xml RollingFile settings
Unfortunately this is not the solution that I'm looking for.