You need to configure a Delete Action. Search for Delete on Rollover in 
http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender 
<http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender>.

Ralph

> On Oct 11, 2021, at 7:05 AM, Alan Franzoni <alan.franz...@gmail.com> wrote:
> 
> Hello, I'm using log4j 2.14.1.
> 
> What I'd like to do is to have an appender which lets me have one log file
> per day, but then delete older logs after N days (e.g. I'd like to have at
> most 10 logfiles, one per day).
> 
> I've tried using the DirectWriteRolloverStrategy, which seems good and
> creates one log file per day when properly configured, but apparently has
> no way of deleting older files, so my log directory gets filled with logs;
> the maxFiles attribute only seems to set ```The maximum number of files to
> allow in the time period matching the file pattern``` (see
> https://logging.apache.org/log4j/2.x/manual/appenders.html) - so, it's only
> useful when there's a size-based policy that rotates logs further.
> 
> The Delete Action only appears to work with the DefaultRolloverStrategy.
> 
> My appender configuration:
> ```
> appender.rolling.type = RollingFile
> appender.rolling.name = ROLLING
> appender.rolling.filePattern = /var/log/application-%d{yyyy-MM-dd}.log
> appender.rolling.layout.type = PatternLayout
> appender.rolling.layout.pattern =
> [%d{yyyy-MM-dd'T'HH:mm:ss,SSS}{UTC}Z][%p][%C:%L] %m%n
> appender.rolling.policies.type = Policies
> appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
> appender.rolling.strategy.type = DirectWriteRolloverStrategy
> appender.rolling.strategy.maxFiles = 10
> ```
> 
> Is there any way to configure the maximum amount (or maximum age) of logs
> in that directory, (ideally) using properties (but if XML is fine if
> properties don't work for this use case) ?
> 
> Thanks,
> 
> Alan

Reply via email to