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
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.
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: