You can use the DailyRollingFileAppender
to rollover based on time. But, it can only,
Is is possible to specify monthly, weekly, half-daily, daily, hourly,
or minutely rollover schedules.
and doesn't seem to have a an option to specify the number of backups. Also,
DailyRollingFileAppender has been observed to exhibit synchronization
issues and data loss. The log4j extras companion includes alternatives
which should be considered for new deployments and which are discussed
in the documentation for org.apache.log4j.rolling.RollingFileAppender.
Ex:
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=app.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd
You can then clear out old files, through a cron job, as mentioned here.Check out the log4j Documentation once.