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.