Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j daily rotation and monthly retention with any file appender

Is it possible with any of the log4j appenders to write the logs with specific rotation time and retention limit.
The goal would be:

  • to have a log file for each day; create a new file at midnight for the new logs
  • to keep the log files and delete them automatically after a certain amount of time; so delete log files older than X days (e.g. 30 days)

It seem that the rotation is possible but the limit of the retention time is not possible with log4j

The log4j version is 1.2.

like image 454
PaulEdison Avatar asked Oct 12 '19 21:10

PaulEdison


People also ask

What is log4j Appender file file?

The log4j. properties file is a log4j configuration file which stores properties in key-value pairs. The log4j properties file contains the entire runtime configuration used by log4j. This file will contain log4j appenders information, log level information and output file names for file appenders.

What is the use of Appender in log4j?

Log4j provides Appender objects which are primarily responsible for printing logging messages to different destinations such as console, files, NT event logs, Swing components, JMS, remote UNIX syslog daemons, sockets, etc.

What is JMS Appender in log4j?

Log4j allows logging requests to print to output destination, which is called an appender. JMS (Java Message Service) is one of Log4j appenders. JMSAppender publishes events to JMS and can perform JNDI requests that leading to possible vulnerability.

How can I get log4j to delete old rotating log files?

You can achieve it using custom log4j appender. MaxNumberOfDays - possibility to set amount of days of rotated log files.


1 Answers

Most of the answers are based on logback. But the question is about log4j 1.2 (old...)

the answer mentioning DailyRollingFileAppender will not work either. org.apache.log4j.DailyRollingFileAppender does not support the MaxBackupIndex property see http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html (this is for RollingFileAppender )

you may be interested in : Use MaxBackupIndex in DailyRollingFileAppender -log4j

and for a possible answer : Log4j Rollingpolicy and MaxbackupIndex

But you should probably use the slf4j log4j "emulation" (http://www.slf4j.org/legacy.html#log4j-over-slf4j) and route your log through logback (without any code change) where it's far easier to implements.

like image 156
benbenw Avatar answered Sep 30 '22 17:09

benbenw