0

Problem: Delete log4j logs after n days.

Constraints:

  • Log4j v1
  • No extra packages (such as log4j extras) Edit: log4j-extras seems available.
  • Solution must be within the log4j configuration (can't use external scripts to delete logs)

I've been trying to use the RollingFileAppender, but it seems i'm not configuring it properly.

Original version of the config file (which is currently used, and not working):

# File appender
log4j.appender.out=org.apache.log4j.DailyRollingFileAppender
log4j.appender.out.DatePattern='.'yyyy-MM-dd
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=%d{ABSOLUTE} | %-5.5p | %-16.16t | %-32.32c{1} | %X{bundle.id} - %X{bundle.name} - %X{bundle.version} | %m%n
log4j.appender.out.file=/var/log/karaf/karaf.log
log4j.appender.out.append=true
log4j.appender.out.maxFileSize=150MB
log4j.appender.out.maxBackupIndex=10

We can see that the configuration is wrong, using maxFileSize and maxBackupIndex, which can't be used with DailyRollingFileAppender.

I tried using the RollingFileAppender, but no luck so far. For example, using RollingFileAppender, i got this issue:

WARN No such property [rollingPolicy] in org.apache.log4j.RollingFileAppender.

Whereas most of the solutions online used a rollingPolicy.

Regarding the log files themselves, the log file for the current day is karaf.log, and the log files generated every day are of the format karaf.log.[yyyy-MM-dd].

Do you know how to solve this?

Thanks.

Edit: After going through some .jar files, i found that log4j-extras seems to be present. I have access to classes like org.apache.log4j.rolling.RollingFileAppender and org.apache.log4j.rolling.TimeBasedRollingPolicy. I trust that this will give me rolling logs (but this is already the case, by using org.apache.log4j.DailyRollingFileAppender). But i can't still find how to delete based on a specific number of days.

Loïc N.
  • 353
  • 3
  • 17
  • 1
    There is no way to obtain this behavior without violating at least one of your constraints. The `RollingFileAppender` your are trying to configure is the [log4j-extras version](https://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html), not the standard one. – Piotr P. Karwasz Jul 29 '21 at 09:09
  • @PiotrP.Karwasz Thanks for your answer. I've found that i have access to some log4j-extras classes (like rolling.RollingFileAppender). Given that, do you know if a solution is possible? – Loïc N. Jul 29 '21 at 22:35
  • I almost forgot how limited are Log4j 1.2 rolling options. Basically: 1. If you use filenames with date patterns, old files are not deleted (this is also not trivial in Log4j2, cf. [this question](https://stackoverflow.com/q/33237731/11748454)), 2. If you use numbered filenames (`FixedWindowRollingPolicy`), old files are deleted. However there is no independent triggering policy to trigger a rollover every day (cf. [this question](https://stackoverflow.com/q/67124080/11748454)). – Piotr P. Karwasz Jul 30 '21 at 09:19

0 Answers0