Menu

#28 Doubly called PropertyConfigurator differs from log4j

v1.0.3
closed
None
5
2013-03-17
2003-12-02
Park Il
No

When I called PropertyConfigurator.configure() twice with the same properties file, all the appenders are also doubled. So that all log messages are doubled!

I tested the same thing with log4j with below code, and it gave me only one log entry.

import java.util.*;

import org.apache.log4j.*;

public class DoubleConfigure {
    public static void main(String[] args) throws  Exception {
        Properties properties = new Properties();
        properties.put("log4j.rootLogger", "ALL, R");
        properties.put("log4j.appender.R", "org.apache.log4j.ConsoleAppender");
        properties.put("log4j.appender.R.layout",
            "org.apache.log4j.PatternLayout");
        properties.put("log4j.appender.R.layout.ConversionPattern",
            "%d [%t] %-5p %c{1} - %m%n");
        PropertyConfigurator.configure(properties);
        PropertyConfigurator.configure(properties);
        Logger logger = Logger.getLogger("Logger");
        logger.debug("TEST!");
    }
}

Related

Bugs: #235

Discussion

  • David Whetstone

    David Whetstone - 2004-05-07

    Logged In: YES
    user_id=989860

    This is an important one, as it prevents you from properly re-configuring
    a system. You can't remove appenders from a logger via the config files.
    That's not good. I'm going to need to fix this myself for our product. I'll
    submit my suggested changes when I do - Unless someone's already
    working on it. Anyone... Anyone...

     
  • David Whetstone

    David Whetstone - 2004-05-07

    Logged In: YES
    user_id=989860

    This is an important one, as it prevents you from properly re-configuring
    a system. You can't remove appenders from a logger via the config files.
    That's not good. I'm going to need to fix this myself for our product. I'll
    submit my suggested changes when I do - Unless someone's already
    working on it. Anyone... Anyone...

     
  • Václav Haisman

    Václav Haisman - 2008-09-10
    • assigned_to: nobody --> wilx
     
  • Václav Haisman

    Václav Haisman - 2008-12-28

    Have you fixed it in your product? If you have, could you share the fix?

    I have looked into the code and it seems that adding call to removeAllAppenders() right before the loop that calls addAppend() in PropertyConfigurator::configureLogger() is the right fix.

     
  • Václav Haisman

    Václav Haisman - 2008-12-28
    • milestone: --> v1.0.3
    • status: open --> open-accepted
     
  • Václav Haisman

    Václav Haisman - 2009-01-02
    • status: open-accepted --> closed-fixed
     
  • Václav Haisman

    Václav Haisman - 2009-01-02

    I have committed the suggested fix in revision 660 on the PRODUCTIN_1_0_x branch. It will be part of the next release candidate.

     
  • Václav Haisman

    Václav Haisman - 2013-03-17
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,31 +1,25 @@
    -When I called PropertyConfigurator.configure\(\) twice 
    -with the same properties file, all the appenders are also 
    -doubled.
    -So that all log messages are doubled\!
    +When I called `PropertyConfigurator.configure()` twice with the same properties file, all the appenders are also doubled. So that all log messages are doubled!
    
    -I tested the same thing with log4j with below code, and 
    -it gave me only one log entry.
    +I tested the same thing with log4j with below code, and it gave me only one log entry.
    
    -import java.util.\*;
    +~~~~
    +import java.util.*;
    
    -import org.apache.log4j.\*;
    +import org.apache.log4j.*;
    
    -public class DoubleConfigure \{
    -public static void main\(String\[\] args\) throws 
    -Exception \{
    -Properties properties = new Properties\(\);
    -properties.put\("log4j.rootLogger", "ALL, R"\);
    -properties.put
    -\("log4j.appender.R", "org.apache.log4j.ConsoleAppender"\)
    -;
    -properties.put\("log4j.appender.R.layout",
    -"org.apache.log4j.PatternLayout"\);
    -properties.put
    -\("log4j.appender.R.layout.ConversionPattern",
    -"%d \[%t\] %-5p %c\{1\} - %m%n"\);
    -PropertyConfigurator.configure\(properties\);
    -PropertyConfigurator.configure\(properties\);
    -Logger logger = Logger.getLogger\("Logger"\);
    -logger.debug\("TEST\!"\);
    -\}
    -\}
    +public class DoubleConfigure {
    +    public static void main(String[] args) throws  Exception {
    +        Properties properties = new Properties();
    +        properties.put("log4j.rootLogger", "ALL, R");
    +        properties.put("log4j.appender.R", "org.apache.log4j.ConsoleAppender");
    +        properties.put("log4j.appender.R.layout",
    +            "org.apache.log4j.PatternLayout");
    +        properties.put("log4j.appender.R.layout.ConversionPattern",
    +            "%d [%t] %-5p %c{1} - %m%n");
    +        PropertyConfigurator.configure(properties);
    +        PropertyConfigurator.configure(properties);
    +        Logger logger = Logger.getLogger("Logger");
    +        logger.debug("TEST!");
    +    }
    +}
    +~~~~
    
     

Log in to post a comment.