aspose file tools*
The moose likes Other Open Source Projects and the fly likes Using Log4j efficiently 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 "Using Log4j efficiently" Watch "Using Log4j efficiently" New topic
Author

Using Log4j efficiently

Nadeem Awad
Ranch Hand

Joined: Jan 09, 2005
Posts: 74
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 ]

Share Knowledge to gain it.<br /> <br />SCJP 1.2, SCDJWS 1.4, SCWCD 1.4, SCBCD 1.3, ICAD
Nadeem Awad
Ranch Hand

Joined: Jan 09, 2005
Posts: 74
Any ideas? Anyone?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41800
    
  62
Why do you consider it troubling to have a log field in all classes where you want to do logging?


Ping & DNS - my free Android networking tools app
Nadeem Awad
Ranch Hand

Joined: Jan 09, 2005
Posts: 74
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

Joined: Oct 10, 2000
Posts: 1364
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Log4j efficiently