Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j2 IfLastModified condition in a Delete action is not interpreting age duration correctly

Tags:

java

log4j2

I can't seem to get the IfLastModified condition in my log4j Delete action to properly interpret an age set in months. It keeps using it as minutes.

I have this Delete action in my log4j XML configuration file.

<DefaultRolloverStrategy>
   <Delete basePath="${logging.dir}" maxDepth="1">
      <IfFileName glob="framework-${hostName}-*.log" />
      <IfLastModified age="2m" />
   </Delete>
</DefaultRolloverStrategy>

Unfortunately, it keeps using the age as minutes as seen from the logs:

createDeleteAction(basePath="./build/logs", followLinks="false", maxDepth="1", testMode="false", PathSorter=null, ={IfFileName(glob:framework-SY4-9CMY2W2-*.log), IfLastModified(age=PT2M)}, ScriptCondition=null, Configuration...

According to the Duration specification https://en.wikipedia.org/wiki/ISO_8601#Durations "To resolve ambiguity, "P1M" is a one-month duration and "PT1M" is a one-minute duration (note the time designator, T, that precedes the time value)"

I tried specifying the age using different Duration formats but the same result.

<IfLastModified age="P2m" />
...
<IfLastModified age="P2M" />

I know I can specify 60D but I'm wondering why I can't use months duration.

I am using log4j 2.12.1

Furthermore, I suspected it could be because the filePattern was in minutes.

filePattern="${logging.dir}/framework-${hostName}-%d{yyyy-MM-dd-HH-mm}.log"

However, that did not have any effect.

like image 312
swasweng Avatar asked Dec 10 '25 00:12

swasweng


1 Answers

Log4j2 Duration object does not support month format in Duration. That must be the reason for this behaviour. It is documented here in API Documentation -

https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/appender/rolling/action/Duration.html

Simplified implementation of the ISO-8601 Durations standard. The supported format is PnDTnHnMnS, with 'P' and 'T' optional. Days are considered to be exactly 24 hours.

Similarly to the java.time.Duration class, this class does not support year or month sections in the format. This implementation does not support fractions or negative values.

like image 128
SKumar Avatar answered Dec 11 '25 14:12

SKumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!