You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Tukesh (Jira)" <ji...@apache.org> on 2022/08/16 15:52:00 UTC

[jira] [Created] (LOG4J2-3575) PropertyConfigurator.configureAndWatch overrides existing LoggerContext and Appenders.

Tukesh created LOG4J2-3575:
------------------------------

             Summary:  PropertyConfigurator.configureAndWatch overrides existing LoggerContext and Appenders. 
                 Key: LOG4J2-3575
                 URL: https://issues.apache.org/jira/browse/LOG4J2-3575
             Project: Log4j 2
          Issue Type: Bug
          Components: Log4j 1.2 bridge
            Reporter: Tukesh


Consider scenarios when we have 2 log4j.properties file with same Java Program. One log4j.properties file is passed as log4j.configuration where as another file is loaded in using 
PropertyConfigurator.configureAndWatch.  In this case, PropertyConfigurator.configureAndWatch overrides original log4j.properties file.

-Dlog4j.configuration=file:conf/log4j.properties  -Dlog4j1.compatibility=true 

 

 
{code:java}
public class TEST4 {    /** Specifies the Logger */
    private static final Logger log = Logger.getLogger(TEST4.class.getName());    public static void main(String[] args) {
        // TODO Auto-generated method stub
        log.info("info");
        log.error("error");
        String configFile = "conf/JTAPILog4j.properties";
        PropertyConfigurator.configureAndWatch(configFile, 60000);         log.info("info");
        log.error("error");    }
} {code}
 Possible suspect :-
PropertyWatchdog  constructs calls constructor of FileWatchdog and FileWatchdog  constructors calls doOnChange() method which is overriding by PropertyWatchdog. However PropertyWatchdog's classLoader is still which is why "PropertyConfigurator().doConfigure" creates new LoggerContext instead of adding this to original context. 


PropertyConfigurator.java :-
{code:java}
 static class PropertyWatchdog extends FileWatchdog {        private final ClassLoader classLoader;        PropertyWatchdog(final String fileName, final ClassLoader classLoader) {
            super(fileName);
            this.classLoader = classLoader;
        }        /**
         * Call {@link PropertyConfigurator#configure(String)} with the <code>filename</code> to reconfigure log4j.
         */
        @Override
        public void doOnChange() {
            new PropertyConfigurator().doConfigure(filename, LogManager.getLoggerRepository(), classLoader);
        }
    }
 {code}
{code:java}
FileWatchdog.java :-
    

protected FileWatchdog(final String fileName) {         super("FileWatchdog");         this.filename = fileName;         this.file = new File(fileName);         setDaemon(true);         checkAndConfigure();     }    protected void checkAndConfigure() {         boolean fileExists;         try {             fileExists = file.exists();         } catch (final SecurityException e) {             LogLog.warn("Was not allowed to read check file existance, file:[" + filename + "].");             interrupted = true; // there is no point in continuing             return;         }        if (fileExists) {             final long fileLastMod = file.lastModified(); // this can also throw a SecurityException             if (fileLastMod > lastModified) { // however, if we reached this point this                 lastModified = fileLastMod; // is very unlikely.                 doOnChange();                 warnedAlready = false;             }         } else {             if (!warnedAlready) {                 LogLog.debug("[" + filename + "] does not exist.");                 warnedAlready = true;             }         }     }
{code}
 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)