Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-3575

PropertyConfigurator.configureAndWatch overrides existing LoggerContext and Appenders.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • Log4j 1.2 bridge
    • None

    Description

      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 

       

       

      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");    }
      } 

       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 :-

       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);
              }
          }
       
      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;             }         }     }
      

       

      Attachments

        Activity

          People

            Unassigned Unassigned
            TukeshK Tukesh
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: