Trying to set up Log4J 2 in a Java 8 project with the following requirements:
- Each run will output its logs (and eventually some other files as well) to its own unique folder based on the time the run started (e.g. currently thinking something like:
logs/yyyy-MM-dd_HH-mm-ss-SSSZ/output.logto make sure multiple runs on the same day will still get their own unique folders). Other files and reports will get stored in this same folder as the logs, which is why it's important to me that each run gets its own unique folder with the log file inside it - Folders older than a certain time period (say, 30 days) will get deleted automatically without having to go into the folder manually and deleting them yourself
- All the log messages for a given run are encapsulated by the single
output.logfile in that run's specific folder; any file size limits or rollover-indices in the filename is not desirable or needed (but willing to deal with it if it's literally the only possible way to get my desired behaviour)
Example desired behaviour:
logs/
├── 2024-03-06_12-55-30-22-123-0500/
│ └── output.log
├── 2024-03-06_13-47-36-10-213-0500/
│ └── output.log
└── 2024-03-10_16-09-10-33-321-0500/
└── output.log
And then say if the application is ran again ~1 month later, the first 2 folders/logs on March 6th will get deleted because they are too old:
logs/
├── 2024-03-10_16-09-10-33-321-0500/
│ └── output.log
└── 2024-04-08_16-09-10-33-321-0500/
└── output.log
I've been reading a bunch of the docs on Apache's website for Log4J 2 and have been scouring similar questions/answers on the rest of the forum for a couple days at this point. I've got the current configuration which does not completely work how I want:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Properties>
<Property name="basedir">logs</Property>
<Property name="basename">output</Property>
<Property name="dateformat">${date:yyyy-MM-dd_HH-mm-ss-SSSZ}</Property>
<Property name="pattern">[%p|%c{2}|L%L] %d{yyyy-MM-dd'T'HH:mm:ssZ}: %m%n</Property>
</Properties>
<Appenders>
<!-- Log to a file within a unique folder for each run -->
<RollingFile name="ROLLINGFILE" filePattern="${basedir}/${dateformat}/${basename}.log">
<PatternLayout pattern="${pattern}"/>
<Policies>
<!-- Trigger a rollover on startup to hopefully allow deleting old logs? -->
<OnStartupTriggeringPolicy />
</Policies>
<DirectWriteRolloverStrategy>
<!-- Delete logs that are 30 days old FIXME: does not work -->
<Delete basePath="${basedir}" maxdepth="2">
<!--<IfLastModified age="P30D" />-->
<IfLastModified age="PT30S"/> <!-- modified to 30 seconds for testing -->
</Delete>
</DirectWriteRolloverStrategy>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ROLLINGFILE"/>
</Root>
</Loggers>
</Configuration>
This works by writing the logs for each run to its own unique folder, but deleting old logs/folders doesn't work. Both the folders and the log files inside those folders don't get deleted even if they're older than the modified date.
Any help would be greatly appreciated! Currently settling for no automatic deletion and just using a simple <File> appender, but I'd very much like some form of retention policy/automatic deletion to work if possible.