6 Replies Latest reply: Jun 6, 2013 2:23 AM by Kayaman RSS

    Duplicatated meaasges using Log4J

    fatbat
      All,

      I am getting duplicate log messages when I try to log messages using log4j. I am logging events within a Timer app that runs every minute.

      Every time I log a message, the messages keep multiplying.

      I found numerous posts on this issues but they all seem to be resolved by setting additivity to false or removed extra appenders. I don't have those conditions.

      Thanks in advance for you assistance.
      package com.Test.Util;
      
      import org.apache.log4j.ConsoleAppender;
      import org.apache.log4j.Level;
      import org.apache.log4j.Logger;
      import org.apache.log4j.PatternLayout;
      
      public class TestLogger {
      
           public static void logMessage(String pMessage, String pLevel) 
          {   
                Logger myLogger = Logger.getLogger(TestLogger.class);
              PatternLayout myLayout = new PatternLayout("[%c][%l][%p][%thread]: %m%n");
              ConsoleAppender consoleAppender= new ConsoleAppender(myLayout);        
              myLogger.addAppender(consoleAppender);
              myLogger.setAdditivity(false);
              Logger.getRootLogger().setAdditivity(false);
              Logger.getLogger(TestLogger.class).setAdditivity(false);
              Logger.getLogger("com.Test.Util").setAdditivity(false);
              
              if (pLevel.toUpperCase().equals("WARN")){
                   myLogger.setLevel(Level.WARN);
                   myLogger.warn(pMessage);
              } else if (pLevel.toUpperCase().equals("FATAL")){
                   myLogger.setLevel(Level.FATAL);
                   myLogger.fatal(pMessage);
              } else if (pLevel.toUpperCase().equals("ERROR")){
                   myLogger.setLevel(Level.ERROR);
                  myLogger.error(pMessage);
              } else {
                   myLogger.setLevel(Level.INFO);
                  myLogger.info(pMessage);
              }
          }
      }
      Thanks!
        • 1. Re: Duplicatated meaasges using Log4J
          rp0428
          Welcome to the forum!
          >
          I am getting duplicate log messages when I try to log messages using log4j. I am logging events within a Timer app that runs every minute.

          Every time I log a message, the messages keep multiplying.

          I found numerous posts on this issues but they all seem to be resolved by setting additivity to false or removed extra appenders. I don't have those conditions.
          >
          Where is your test case that calls the code you posted and actually shows duplicate messages getting created? No one can try to reproduce your issue without knowing WHAT you are doing and HOW you are doing it.

          There is also no mention at all of what version of the log jar or Java you are using.
          • 2. Re: Duplicatated meaasges using Log4J
            fatbat
            Quite right. Sorry about that.

            Log4j version 1.2.17

            Class with main
            package com.testco;
            
            import com.testco.TestLogger;
            
            public class Test {
                 public static void main(String[] args) {
                      int count = 0;
                      while (count<= 5) {
                           try {
                                TestLogger.logMessage("Count:" + count, "INFO");
                                Thread.sleep(5000);
                                count++;
                           } catch (InterruptedException e) {
                                TestLogger.logMessage(e.getMessage(), "ERROR");
                           }
                      }
                 }
            }
            Logging code
            package com.testco;
            
            import org.apache.log4j.ConsoleAppender;
            import org.apache.log4j.Level;
            import org.apache.log4j.Logger;
            import org.apache.log4j.PatternLayout;
             
            public class TestLogger {
             
                 public static void logMessage(String pMessage, String pLevel) 
                {   
                    Logger myLogger = Logger.getLogger(TestLogger.class);
                    PatternLayout myLayout = new PatternLayout("[%c][%l][%p][%thread]: %m%n");
                    ConsoleAppender consoleAppender= new ConsoleAppender(myLayout);        
                    myLogger.addAppender(consoleAppender);
                    myLogger.setAdditivity(false);
                    Logger.getRootLogger().setAdditivity(false);
                    Logger.getLogger(TestLogger.class).setAdditivity(false);
                    
                    if (pLevel.toUpperCase().equals("WARN")){
                         myLogger.setLevel(Level.WARN);
                         myLogger.warn(pMessage);
                    } else if (pLevel.toUpperCase().equals("FATAL")){
                         myLogger.setLevel(Level.FATAL);
                         myLogger.fatal(pMessage);
                    } else if (pLevel.toUpperCase().equals("ERROR")){
                         myLogger.setLevel(Level.ERROR);
                        myLogger.error(pMessage);
                    } else {
                         myLogger.setLevel(Level.INFO);
                        myLogger.info(pMessage);
                    }
                }
            }
            Output
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:0
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:1
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:1
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:2
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:2
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:2
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:3
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:3
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:3
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:3
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:4
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:4
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:4
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:4
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:4
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:5
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:5
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:5
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:5
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:5
            [com.testco.TestLogger][com.testco.TestLogger.logMessage(TestLogger.java:32)][INFO][mainhread]: Count:5

            Edited by: fatbat on Jun 5, 2013 10:17 PM
            • 3. Re: Duplicatated meaasges using Log4J
              Kayaman
              You should be creating the logger and only once. Now you're adding a new appender to the logger on every call of the method.
              • 4. Re: Duplicatated meaasges using Log4J
                fatbat
                No duplicates when I use the Property Config file. Still curious as to why doing the config in java behaves like this.

                Thanks all
                • 5. Re: Duplicatated meaasges using Log4J
                  gimbal2
                  fatbat wrote:
                  Still curious as to why doing the config in java behaves like this.
                  Huh?
                  Kayaman wrote:
                  You should be creating the logger and only once. Now you're adding a new appender to the logger on every call of the method.
                  That's why.
                  • 6. Re: Duplicatated meaasges using Log4J
                    Kayaman
                    fatbat wrote:
                    Still curious as to why doing the config in java behaves like this.
                    Nothing curious about it. Each time you call the method, you get the logger and add an appender to it. The first time it has zero appenders, so you get one message. The next time it already has the appender you added the last time, so you add a second one.

                    What made you think that setting up the logger in the logging method is a good idea?