Where should I put the log4j.properties file?
I wrote a web service project using netbeans 6.7.1 with glassfish v2.1, put log4j.properties to the root dir of project and use:
static Logger logger = Logger.getLogger(MyClass.class);
and in functions:
logger.info("..."); logger.error("..."); // ...
but, it is error info(actually, I have tried to put it almost every dir that I could realize):
log4j:ERROR Could not read configuration file [log4j.properties]. java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.java:106) at java.io.FileInputStream.<init>(FileInputStream.java:66) at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:297) at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:315) at com.corp.ors.demo.OrsDemo.main(OrisDemo.java:228) log4j:ERROR Ignoring configuration file [log4j.properties]. log4j:WARN No appenders could be found for logger (com.corp.ors.demo.OrsDemo). log4j:WARN Please initialize the log4j system properly.
the example project could be get from http://www.91files.com/?N3F0QGQPWMDGPBRN0QA8
I know it's a bit late to answer this question, and maybe you already found the solution, but I'm posting the solution I found (after I googled a lot) so it may help a little:
- Put log4j.properties under WEB-INF\classes of the project as mentioned previously in this thread.
- Put log4j-xx.jar under WEB-INF\lib
- Test if log4j was loaded: add
-Dlog4j.debug@ the end of your java options of tomcat
Hope this will help.
Read more... Read less...
As already stated, log4j.properties should be in a directory included in the classpath, I want to add that in a mavenized project a good place can be
You can specify config file location with VM argument -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties"
You have to put it in the root directory, that corresponds to your execution context.
MyProject src MyClass.java log4j.properties
If you start executing from a different project, you need to have that file in the project used for starting the execution. For example, if a different project holds some JUnit tests, it needs to have also its log4j.properties file.
I suggest using log4j.xml instead of the log4j.properties. You have more options, get assistance from your IDE and so on...
For a Maven Based Project keep your log4j.properties in src/main/resources. Nothing else to do!
If you put log4j.properties inside src, you don't need to use the statement -
It will be taken automatically as the properties file is in the classpath.