It's not a secret anymore!*
The moose likes Other Open Source Projects and the fly likes log4j logging twice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "log4j logging twice" Watch "log4j logging twice" New topic
Author

log4j logging twice

Mahesh Trikannad
Ranch Hand

Joined: Dec 14, 2004
Posts: 68
Hi All,

I see log4J logging the same information twice.

I call a method ( pasted below ). the method returns me a myLogger which is a instance of the log4j Logger
then I do a

myLogger.fatal(myAct.createStringForFileLogging(stepName));

I do this in a MDB. Also if myLogger is not null, I use myLogger as is

this is how I get the logger

String logPattern = "%m%n";
String datePattern = "'.'yyyy-MM-dd"
Logger myLogger = null;
DailyRollingFileAppender logFile = null;
logFile = new DailyRollingFileAppender(new PatternLayout(logPattern),logFileName, datePattern);
myLogger = Logger.getLogger(loggerName);
myLogger.addAppender(logFile);
myLogger.setLevel(Level.DEBUG);
return myLogger;

Appreciate help on this. Its been driving me crazy


Mahesh Trikannad
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Sounds like an additivity issue. If the specified logger and the root logger both log it you can end up with dups. You could try setting additivity to false on your logger. Log4j manual mentions additivity and has a link to API with the method setAdditivity().

I don't normally configure anything in code, have always used properties files. I tried coding exactly what you said and I didn't get duplicate messages; you may have other configuration elsewhere in your code (like for root logger?). I had to make a couple thing up and I added to the logPattern so I could see the timestamp, level, loggername.


[ March 11, 2005: Message edited by: Carol Enderlin ]
Mahesh Trikannad
Ranch Hand

Joined: Dec 14, 2004
Posts: 68
Thanks Carol,

I had tryed the additivity thingy, but that was not it.

As it turned out, when I made the Logger variable static, it worked Ok.

Previously every MDB instance had its own, Logger variable.
I was under the impression, the getLogger(loggerName) , would return a singleton.

Guess I still dont really understand, why creating 2 different logger variables, and using one to log something, actually logs it twice.

Appreciate your insights

Regards

Mahesh
Vaibhav Puranik
Greenhorn

Joined: Apr 06, 2005
Posts: 1
Mahesh,

This kind of things happen because of class loaders. If you calling getLogger method from ejbs and jsps, it will create two different instances of loggers as both classloaders are different.

Vaibhav
 
GeeCON Prague 2014
 
subject: log4j logging twice