Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

log4j logging twice

 
Mahesh Trikannad
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic