Bug 52168 - change log4j property file at runtime cause an empty default log file is created
Summary: change log4j property file at runtime cause an empty default log file is created
Status: NEW
Alias: None
Product: Log4j - Now in Jira
Classification: Unclassified
Component: Appender (show other bugs)
Version: 1.2
Hardware: PC All
: P2 normal
Target Milestone: ---
Assignee: log4j-dev
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-10 20:16 UTC by Alessio Pollero
Modified: 2011-11-10 20:18 UTC (History)
1 user (show)



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alessio Pollero 2011-11-10 20:16:13 UTC
I have an application that uses log4j to log on a text file, i have placed a log4j.properties file inside my executable jar file containing the default log configuration parameters.

My log4j.properties file looks like the following :

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 have also a command line option that allow the user to change the log file name, so i have a function like the following that change the settings of log4j 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 work fine, unless when the statement LogManager.resetConfiguration() and/or PropertyConfigurator.configure(props) are executed a a default empty log file (mylogfile.log) is automatically created. After that everything i log from my application will be written correctly in the new file, but an empty file is created anyway (with the name of the default log filename in the log4j.properties).