Step-by-step what is happening here is:

First-call:
1) A logger with a name matching the given class_name does not exist, so
log4j creates it.
2) Two appenders are added to the logger (a console appender and a file
appender).

Second-call:
1) A logger with a name matching the given class name already exists (it was
created above), so that logger is returned (including the two appenders
previously attached)
2) Two appenders are added to the logger (a console appender and a file
appender).

Etc...


Assuming that there is a vaild reason you need to wrap all these log4j calls
inside your own hand-rolled class, then try something like the following
code change in the constructor:


public Trace( String class_name )
{
        if (!LogManager.exists( class_name ))
        {
                logger = LogManager.getLogger(class_name);
        Layout layout = new PatternLayout("%d{yyyyMMdd-HH:mm:ss} %m%n");
        logger.addAppender(new ConsoleAppender(layout,
ConsoleAppender.SYSTEM_OUT));
              logger.setLevel(Level.DEBUG);
        try 
                {
                logger.addAppender(new FileAppender(layout,
(String)Ecx_constants.getInstance().get("log4j_fileTrace"), true));
              }
                catch (IOException e)
                {}
        }
}

But the better solution is to perform initialization of the log4j library
through config files.





    |-----Original Message-----
    |From: Sébastien Hiblot [mailto:[EMAIL PROTECTED]]
    |Sent: Thursday, January 16, 2003 7:22 AM
    |To: [EMAIL PROTECTED]
    |Subject: Help !
    |
    |
    |Hi,
    |
    |We're working on a java web site and we'd like to use 
    |log4j in order
    |to log the error.
    |
    |We have made a class to implement the init of log4j. Here 
    |the source of this
    |class :
    |
    |private Logger logger ;
    |
    |public Trace(String class_name) {
    |   logger = LogManager.getLogger(class_name);
    |      Layout layout = new 
    |PatternLayout("%d{yyyyMMdd-HH:mm:ss} %m%n");
    |      logger.addAppender(new ConsoleAppender(layout,
    |ConsoleAppender.SYSTEM_OUT));
    |      logger.setLevel(Level.DEBUG);
    |      try {
    |           logger.addAppender(new FileAppender(layout, (String)
    |Ecx_constants.getInstance().get("log4j_fileTrace"), true));
    |      } catch (IOException e) {
    |}
    |
    |To log something :
    |Trace t = new Trace(this.getClass().toString()) ;
    |t.debug("My message");
    |
    |The first time, we call the page, the log contain the good thing :
    |20030116-12:00:00 My message
    |
    |But the second time we call the page, 30 sec after, we 
    |have in the log:
    |20030116-12:00:00 My message
    |20030116-12:00:30 My message
    |20030116-12:00:30 My message
    |
    |==> the log appears 2 times !
    |the third time, the log appears 3 time, etc......
    |
    |Can you help us please ?
    |
    |Thx
    |
    |
    |--
    |To unsubscribe, e-mail:   
    |<mailto:[EMAIL PROTECTED]>
    |For additional commands, e-mail: 
    |<mailto:[EMAIL PROTECTED]>
    |

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to