You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Piotr Karwasz (Jira)" <ji...@apache.org> on 2022/09/07 18:54:00 UTC

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

    [ https://issues.apache.org/jira/browse/LOG4J2-3575?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17601477#comment-17601477 ] 

Piotr Karwasz commented on LOG4J2-3575:
---------------------------------------

Hi [~TukeshK],

Thank you reporting this issue. As you notice correctly the Log4j 1.x bridge works as if {{log4j.reset = true}} was specified in every configuration. We didn't implement the configuration merging capability.

This could be implemented in terms of Log4j2 {{{}CompositeConfiguration{}}}. However we are quite busy now with the 2.x releases and the upcoming 3.x series, so a PR would be welcome. :D

>  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
>            Priority: Major
>
> 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)