• 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

logging instance

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • 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: 24963
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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: 24963
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the code in a static method is executed every time that method is called.
 
Paul Clapham
Marshal
Posts: 24963
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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: 24963
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!