This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

log4j logging twice

 
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
 
drifter
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
 
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
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!