Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I overwrite a log file in log4j?

Tags:

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 ?

like image 758
Geo Avatar asked Jun 08 '09 15:06

Geo


People also ask

Where is log4j log file location?

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.

What is rolling file in log4j?

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.

How do I change the log4j properties at runtime?

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.


1 Answers

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).

like image 93
matt b Avatar answered Nov 18 '22 20:11

matt b