This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • 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

PropertyConfigurator in log4j

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
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!