I am trying to use log4j in a project I have. This project is a mix of servlets and regular classes. Currently, in every class that needs to user a Logger, I do 2 steps:
1. Make a logger object for that class
2. Configure it with PropertyConfigurator and the log4j.properties file i use
Information on the correct use or common usage of log4j seems sparse other than the apache page (or maybe my google skills are just lacking), but I wanted to know is this an accepted way of using log4j? Is there a way to simply configure once in the whole project and just make a new Logger object for every class that needs it?
No issue specifically. I was just wondering if that was a common way of doing things.
Well actually, there is one issue. I was going to post a new topic about this, but maybe I will just post here. How do you reference your .properties file? Like what file path do you use?
This whole project is run in tomcat, so right now, for the PropertyConfigurator.configure() parameter, i use something like "../webapps/ProjectName/WEB-INF/classes/resources/log4j.properties", with the appropriate slashes for the environment, but that strikes me as not very portable. I'm betting on the fact that Tomcat has to be started in the TOMCAT_HOME/bin directory, but is there a better, more elegant way to handle this?
Bob Homes wrote:How do you reference your .properties file?
I don't. I drop the file into Tomcat's /lib folder and forget about it.
Forget about a configurator. Just drop the Log4J jar file and the commons logging jar file into the /lib folder along with the log4j.pproperties file, and let it do the rest.
Ok that worked. But instead of putting it in the lib folder, I put it in a package so it ends up in the classes folder of the project. This way the file doesn't have to be placed anywhere manually by the user.