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 days of log).
I've tried using the DirectWriteRolloverStrategy, which seems good and creates one log file per day, 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). 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 = 3
Is there any way to configure the maximum amount (or maximum age) of logs in that directory, using properties?
So, after a bit of fiddling, and with some help from Volkan and Ralph, I was able to discover that the Delete action works for DirectWriteRolloverStrategy as well, even though I wasn't provided a working piece of code. I figured out that the properties go like this for what I requested:
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.action.type = Delete
appender.rolling.strategy.action.condition.type = IfFileName
appender.rolling.strategy.action.basepath = /var/log
appender.rolling.strategy.action.maxdepth = 1
appender.rolling.strategy.action.condition.glob = application*.log
appender.rolling.strategy.action.ifAccumulatedFileCount.type = IfAccumulatedFileCount
appender.rolling.strategy.action.ifAccumulatedFileCount.exceeds = 10
I'll contribute the fix to the doc and the example back to the log4j community.