• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Log4j efficiently

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any ideas? Anyone?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic