I want the log to roll over as long as the application is running, but I want the log to start fresh when the application is restarted.
Updated: Based on erickson's feedback, my appender looks like this:
<appender name="myRFA" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="my-server.log"/> <param name="Append" value="false" /> <param name="MaxFileSize" value="10MB"/> <param name="MaxBackupIndex" value="10"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{ISO8601} %p - %t - %c - %m%n"/> </layout> </appender>
I simply added the following line:
<param name="Append" value="false" />
It now truncates the base log file at startup, but it leaves the rolled files alone.
Note that log4j2 does have a method for deleting old log files via the DefaultRolloverStrategy – but it is not in the sample log4j2. xml file that is distributed. The 50 days comes from age=”50d” which can be changed.
The Log4j logging settings are stored in the file app_data /conf/server/log4j.
You can safely remove these files. Also, these files don't include the log4j-core-2.7. jar which is the jar containing the vulnerability, so there is no exploit risk.
By default, log4j logging is used for all components for which logging information is generated. To disable log4j logging, set the logging level for the component to OFF in both log4j. conf and log4j.
If you set the append
parameter to false
, the base log file will be "started fresh" when the application restarts. Do you mean that you want any "rolled" log files to be deleted too?
I've written some custom code to find my RollingFileAppender
(which is unnecessarily difficult to get access to in log4j!) which I then cause to roll over. I've adapted my code below for a single use. I use code similar to this at application startup to force my logs to roll (if non-empty) so I always start in a fresh log but never delete any log but the oldest.
This code takes a given Logger and loops up the logger hierarchy until it finds a Logger that has Appenders attached. If it never does, then it gives up. If it does, it loops over all Appenders attached to that Logger and for each one that is a RollingFileAppender, it forces the log to roll.
Something like this should be a lot easier to do in log4j, but I haven't found a simpler way of doing it.
public void rollLogFile(Logger logger) { while (logger != null && !logger.getAllAppenders().hasMoreElements()) { logger = (Logger)logger.getParent(); } if (logger == null) { return; } for (Enumeration e2 = logger.getAllAppenders(); e2.hasMoreElements();) { final Appender appender = (Appender)e2.nextElement(); if (appender instanceof RollingFileAppender) { final RollingFileAppender rfa = (RollingFileAppender)appender; final File logFile = new File(rfa.getFile()); if (logFile.length() > 0) { rfa.rollOver(); } } } }
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