• 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

logging instance

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I usually see that each class has its private static final instance of logger. I'm trying a more extreme approach


and all the class that needs logging simply issues this:


are there any side effects with this approach?
thanks
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two. First you will be calling PropertyConfigurator.configure every time you log something; the usual way would be to just call it in some initialization code. And second, you will be throwing runtime exceptions and terminating your code every time you log an error or debug.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll be calling PropertyConfigurator everytime I log something? But, I used static, so shouldn't it be only run once when the class is loaded?
what is the sample best practice for logging code?
thanks
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the code in a static method is executed every time that method is called.
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for "best practices"... I'm just going to say it's a good idea to just put your log4j configuration file into the classpath and let log4j find it the first time you do any logging. I'm not going to raise that to the lofty status of "best practice" but I'm just going to say it's a good idea.
 
David Spades
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I'll just stick to the old ways : one logger per class.
okay, so what should I do to make sure the properties file is only loaded once?
thanks
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you really want a static method which is only called once, that's what a static initializer is for.

As for how to make sure the properties file is only loaded once... that was the subject of my previous post.
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another problem with this approach is that it makes problem diagnosis - which is the main reason we use logging - much harder.
If message pattern has %c or %L, every message will have category/class as "MyLogger" and corresponding line number of MyLogger, instead of the actual invoking class and line number.
Every logging call will then have to explicitly pass its class name / method name / line number to MyLogger.
reply
    Bookmark Topic Watch Topic
  • New Topic