Clear rotated logs on startup in log4j using RollingFileAppender

Posted on 2010-08-20
Last Modified: 2012-05-10
I have my log4j config set up with MaxBackupIndex=1.  When I close down the app then bring it up, it clears the main log file (which is what I want), but does not delete any rolled files from the previous run (this is what I want to change).

For example, if the log file is gui.log and I close the gui when gui.log and gui.log.1 exist, then I bring up the GUI again, I want gui.log cleared (which is happening) and gui.log.1 deleted (what I'm trying to figure out how to do).

Any suggestions would be most appreciated.  Thanks!
Question by:stev0931
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Assisted Solution

CEHJ earned 200 total points
ID: 33485465
You might need something custom for that

Author Comment

ID: 33485958
Doing something custom is always an option, but I'd prefer to use something via the log4j API first for reduced system fragility.
LVL 16

Expert Comment

ID: 33486456
try to set parameter Append to false. I think that it will erase the old files too...?!
If you don't want old files, then set MaxBackupIndex=0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users


Author Comment

ID: 33486522
Setting append to false results in the main log file (e.g. gui.log) being cleared at startup.  Unfortunately, it does not impact the gui.log.1.  Setting MaxBackupIndex to 0 will get me no log rolling at all.  I need to have log rolling - I just need to delete old rolled logs on application startup.
LVL 92

Accepted Solution

objects earned 300 total points
ID: 33490086
log4j only manages the file it is currently logging to.
Usual practice is to manage logs externally to log4j eg. using cron or doing it in your startup script.
LVL 86

Expert Comment

ID: 33490646
There is no ready made way that log4j does this, but you could use a custom Configurator

Author Closing Comment

ID: 33491530
Thanks!  Not what I wanted to hear, but sometimes the answer just works out that way ;)

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question