I have a log file that has the following appender added to it :
logger.addAppender(new FileAppender(new PatternLayout(),"log.txt"));
the thing is, each time I'm running my application, additional logging information gets appended to the same log file. What can I do to overwrite the file each time ?
Use RollingFileAppender.
The previous answer by Matt is correct except that it uses a properties file. If you are looking for a programmatic approach, I suggest that you disable append mode by modify your code as follows:
Add to your XML file the following line:
Note that due to odd XML parsing in log4j, the
<param>
elements must appear in a block (not intermingled with other types of elements).For example, this works:
But this doesn't (!)
If you have an appender declared like so in a properties file:
Then what you want to add is
The default value is
true
.So, if you are declaring your appenders programmatically, then what you want to do is call
setAppend(false)
.