Clear rotated logs on startup in log4j using RollingFileAppender

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!
stev0931Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
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.
0
 
CEHJConnect With a Mentor Commented:
You might need something custom for that
0
 
stev0931Author Commented:
Doing something custom is always an option, but I'd prefer to use something via the log4j API first for reduced system fragility.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
ValeriCommented:
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
0
 
stev0931Author Commented:
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.
0
 
CEHJCommented:
There is no ready made way that log4j does this, but you could use a custom Configurator
0
 
stev0931Author Commented:
Thanks!  Not what I wanted to hear, but sometimes the answer just works out that way ;)
0
All Courses

From novice to tech pro — start learning today.