How to configure log4j to only store log files for the last seven days?

I have the following logging issue with multiple Java applications using log4j for logging:

I want the log files to change daily, for example

 log.2010-09-10 log.2010-09-09 log.2010-09-08 log.2010-09-07 log.2010-09-06 log.2010-09-05 log.2010-09-04 

But for data security reasons, we are not allowed to store log files for more than seven days in my company. Therefore, the generation of the next next log file, log.2010-09-11 should initiate the removal of log.2010-09-04 . Is it possible to customize this behavior with log4j ? If not, do you know yet another elegant solution to this registration problem?

+42
java logging log4j
Sep 10 '10 at 9:02
source share
13 answers

You can run your household in a separate script that you can run daily. Something like that:

 find /path/to/logs -type f -mtime +7 -exec rm -f {} \; 
+23
Sep 10 '10 at 9:59
source share

I assume you are using RollingFileAppender? In this case, it has a property called MaxBackupIndex , which you can set to limit the number of files. For example:

 log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=7 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 
+44
Sep 10 '10 at 9:08
source share

According to the following post, you cannot do this with log4j : Use MaxBackupIndex in DailyRollingFileAppender -log4j

As far as I know, this functionality should have turned into log4j 2.0, but this effort has been lost. According to the logback site, logback is the intended successor to log4j, so you can use it.

There is an API called SLF4J that provides a generic logging API. It will load the actual logging implementation at runtime, so depending on the configuration you provided, it may use java.util.log or log4j or logback or any other library that can provide logging facilities. It will be a little forward to move from using log4j directly to using SLF4J, but they provide some tools to automate this process. After you convert your code to SLF4J, switching logs to the backend should just be a replacement for the configuration file.

+10
Sep 10 '10 at 9:53
source share

log2j now has support for removing old logs. See the DefaultRolloverStrategy tag and the snapshot below. It creates up to 10 archives on the same day, will analyze the $ {baseDir} directory that you define in the Properties tag with a maximum depth of 2 with the log file name corresponding to "app - *. Log.gz", and delete logs older than 7 days but keep the last 5 magazines if your last 5 magazines are older than 7 days.

  <DefaultRolloverStrategy max="10"> <Delete basePath="${baseDir}" maxDepth="2"> <IfFileName glob="*/app-*.log.gz"> <IfLastModified age="7d"> <IfAny> <IfAccumulatedFileCount exceeds="5" /> </IfAny> </IfLastModified> </IfFileName> </Delete> </DefaultRolloverStrategy> 
+5
Mar 01 '17 at 15:17
source share

There is also a DailyRollingFileAppender; http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html

Edit: After reading this alarming statement,

It is observed that DailyRollingFileAppender demonstrates the synchronization of the problem and data loss. The additional log4j companion includes alternatives that should be considered for new deployments, which are discussed in the documentation for org.apache.log4j.rolling.RollingFileAppender.

from the above URL (which I never understood before), then it looks like the best bet; http://logging.apache.org/log4j/companions/extras/apidocs/index.html

+4
Sep 10 '10 at 9:47
source share

I stumbled upon this appender here , which does what you want, it can be configured to store a certain number of files that have been turned over by date.

Download: http://www.simonsite.org.uk/download.htm

Example (groovy):

 new TimeAndSizeRollingAppender(name: 'timeAndSizeRollingAppender', file: 'logs/app.log', datePattern: '.yyyy-MM-dd', maxRollFileCount: 7, compressionAlgorithm: 'GZ', compressionMinQueueSize: 2, layout: pattern(conversionPattern: "%d [%t] %-5p %c{2} %x - %m%n")) 
+3
Jun 24 '14 at 13:24
source share

There is another option DailyRollingFileAppender . but it lacks the automatic delete function (save 7 days) that you are looking for

Example

 log4j.appender.DRF=org.apache.log4j.DailyRollingFileAppender log4j.appender.DRF.File=example.log log4j.appender.DRF.DatePattern='.'yyyy-MM-dd 

Something calls me org.apache.log4j.CompositeRollingAppender , which combines both the RollingFileAppender functions (maxSizeRollBackups, backup file number) and the DailyRollingFileAppender ( daily rollup ).

But did not try it, it seems, not a standard log4j 1.2 branch function.

+1
Sep 10 '10 at 9:55
source share

If you are using Linux, you can configure the cron job with tmpwatch.

Most Linux systems have a cron tmpwatch job that cleans the / tmp directory. You can add another one that monitors your logging directory and deletes files within 7 days.

If you are using a different system, there may be equivalent utilities.

+1
Sep 10 '10 at 9:57
source share

Use the log4j.appender.FILE.RollingPolicy.FileNamePattern parameter, for example. log4j.appender.FILE.RollingPolicy.FileNamePattern=F:/logs/filename.log.%d{dd}.gz for logging a month before rolling over.

I did not expect a check for one month, but I tried using mm (i.e. minutes) and confirmed that it is being overwritten, so I assume that it will work for all templates.

+1
Mar 22 '16 at 3:25
source share

The DailyRollingFileAppender class uses the DatePattern parameter to indicate a moving chart. This template must comply with Std's SimpleDateFormat conventions . Edition v1.4.2 . Therefore, we must use the option E (Day of the week). For example:

 <param name="DatePattern" value="'.'EEE"/> 

Read more about the DailyRollingFileAppender class from log4j javadoc here . Unfortunately, the Java 1.4.2 documentation is no longer online, but you can download a copy here .

0
Mar 27 '14 at 18:36
source share

I have installed:

 log4j.appender.R=org.apache.log4j.DailyRollingFileAppender log4j.appender.R.DatePattern='.'yyyy-MM-dd # Archive log files (Keep one year of daily files) log4j.appender.R.MaxBackupIndex=367 

Like the others in front of me, the DEBUG option showed me an error:

log4j: WARN There is no such property [maxBackupIndex] in org.apache.log4j.DailyRollingFileAppender.

Here is an idea that I have not tried yet, suppose I set DatePattern so that the files overwrite each other after the required period of time. To save the year, I could try installing:

 log4j.appender.R.DatePattern='.'MM-dd 

Will this work or will it lead to an error? For example, it will take you a year to find out if I could try:

 log4j.appender.R.DatePattern='.'dd 

but it still takes a month to find out.

0
Sep 30 '14 at 18:08
source share

Despite running the chrone task, we can use the log4j2.properties file in the config logstash folder for the task. Look at the link below, it will be useful.

https://github.com/elastic/logstash/issues/7482

0
Nov 15 '17 at 8:55
source share

I create this method and call it, closing the application:

  public void deleteFiles(){ File f = new File("log"); File[] fileArray = f.listFiles(); double timenow = System.currentTimeMillis(); double olderTenDays = timenow - 864000000;// MS for ten days for (int i = 0; i < fileArray.length; i++) { if(fileArray[i].lastModified()< olderTenDays ) fileArray[i].delete(); } } 
-2
May 24 '14 at 17:01
source share



All Articles