• 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
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Using Log4j efficiently

 
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?
 
Rancher
Posts: 43016
76
  • 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
 
drifter
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.
 
Get me the mayor's office! I need to tell him about this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic