• 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

PropertyConfigurator in log4j

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am inexperienced with log4j, so bear with me. If I understand correctly, if I want to using a property file to set the log level, I need to use the PropertyConfigurator class and use one of the configure() methods, and I should call this in every class that will be using logging. Does this mean that the properties file is going to be read from each time the class is instantiated? Surely, it does not work this way. Does anyone know the answer to this?
Many thanks,
Barry
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To configure Log4J, simply execute one line of code.

In the above example, prop is a Properties instance. You can create it at runtime, read it from a file or get it from a stream whatever is best for you.

From ANYWHERE in ANY code, the following one line of code will get a Logger object, which can be used to log messages.

Using the class name as the logger name is recommended as it allows you, through the Properties file, to redirect logging for one particular class (say a servlet) to a seperate file.
The static method getLogger knows what to do based on what the configure method did previous to it running.
The ability to find the correct log without making any coding changes is one advantage of using Log4j.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok... Just to make sure I understand you. I only need to do the configure once, when the application starts? I don't need to configure() if every single class, right? Because if that is not true, then the properties file would have to be read in every single class, which would be a big waste.
Hey, you wouldn't happen to have an answer for my other post in this same forum, would you?
thanks,
Barry
 
I once met a man from Nantucket. He had a tiny ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic