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 ?
The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.
Java Logging. Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.
Use LogManager. resetConfiguration(); to clear the current config and configure it again. Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config). This way, all the loggers (and their levels, etc) stay intact.
If you have an appender declared like so in a properties file:
log4j.appender.LOGFILE=org.apache.log4j.FileAppender log4j.appender.LOGFILE.File=file.log log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout log4j.appender.LOGFILE.layout.ConversionPattern=%d %-5p %c - %m%n
Then what you want to add is
log4j.appender.LOGFILE.Append=false
The default value is true
.
So, if you are declaring your appenders programmatically, then what you want to do is call setAppend(false)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With