• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Log4j efficiently

 
Nadeem Awad
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've been using Log4j, and I found it a little troublesome to declare an instance variable everywhere and pass the class name to it; like this:



So I thought I could create a wrapper class to encapsulate the logger. For instance:



But I also didn't like passing the class object every time I want to log a message. So the following came to my mind:



So the other classes do the following:



My question is, is this approach efficient? especially that the ex.fillInStackTrace() method is synchronized, so it might be a bit costly.

Any ideas?
[ February 04, 2007: Message edited by: Nadeem Awad ]
 
Nadeem Awad
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any ideas? Anyone?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you consider it troubling to have a log field in all classes where you want to do logging?
 
Nadeem Awad
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Why do you consider it troubling to have a log field in all classes where you want to do logging?


I really find it better to create a wrapper class for the Logger and declare some static methods to be accessed directly from the methods, rather than creating a Logger object in each class. I believe the old fashin is a bit messy, that's why I decided to create the wrapper.

Anyway, for more info check out this thread:
http://forum.java.sun.com/thread.jspa?messageID=9484262
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you realize that java.util.logging package uses the same (old-fashioned) style as log4j?



Using the wrapper class will mean you lose some of the features of log4j. log4j can include location information in the output; the documentation (see PatternLayout) warns about the performance impacts of including that information so you probably would not use it on a regular basis, but it might be helpful to track down a problem. With your setup the location information would point to your wrapper class.

Using the wrapper class only to pass in the class does sound better than your other idea to get the class name out of the stack trace.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic