I have an application that uses log4j to enter a text file, I put the log4j.properties file in my jar executable containing the default log configuration settings.
My log4j.properties file is as follows:
log4j.rootLogger=INFO, A1 log4j.appender.A1=org.apache.log4j.FileAppender log4j.appender.A1.File=mylogfile.log log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d{dd/MM/yyyy HH\:mm\:ss,SSS} %-5p [%t] - %m%n
My application also has a command line parameter that allows the user to change the name of the log file, so I have a function like the following that changes the log4j settings at runtime:
public void changeLogFileName(String filename) { props.setProperty("log4j.appender.A1","org.apache.log4j.DailyRollingFileAppender"); props.setProperty("log4j.appender.A1.DatePattern","'-'ddMMyyyy"); props.setProperty("log4j.appender.A1.File",filename); LogManager.resetConfiguration(); PropertyConfigurator.configure(props); }
Everything works fine, unless the LogManager.resetConfiguration () operator and / or PropertyConfigurator.configure (details) does not execute an empty default log file (mylogfile.log) is automatically created. After that, everything that I register from my application will be correctly written in the new file, but an empty file will be created in any case (with the name of the default log file in log4j.properties).
Is there a way to avoid creating an empty default log file?
source share