views:

3852

answers:

5

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);

in Constructor: PropertyConfigurator.configure("log4j.properties");

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.(FileInputStream.java:106) at java.io.FileInputStream.(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

+2  A: 

The file should be located in the WEB-INF/classes directory. This directory structure should be packaged within the war file.

Martin OConnor
in theory, it is the answer, but the problem is log4j.properties is losted when glassfish deploy war file.
mono
It should be in WEB-INF/classes and packaged within the war file
Martin OConnor
@mono - at least in the downloadable archive there is NO log4j.properties in web/WEB-INF/classes. Martin is right, that's where it should be.
fvu
+2  A: 

You have to put it in the root directory, that corresponds to your execution context.

Example:

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

KLE
in example, I almost put it every directory. but it is not helpful to Glassfish deploy project, because GF don't copy properties to the correct directory. It seems GF do lose it.
mono
actually, I think the problem is from auto-deploy of netbeans. I try to deploy by war file in admin console, it's ok. I don't know if I was right. If I was right, why netbeans don't deploy these files when auto-deploy the project.
mono
A: 

You can specify config file location with VM argument -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties"

Makatun
A: 

A few technically correct specific answers already provided but in general, it can be anywhere on the runtime classpath, i.e. wherever classes are sought by the JVM.

This could be the /src dir in Eclipse or the WEB-INF/classes directory in your deployed app, but it's best to be aware of the classpath concept and why the file is placed in it, don't just treat WEB-INF/classes as a "magic" directory.

Brian
A: 

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:

  1. put log4j.properties under WEB-INF\classes of the project as mentioned previously in this thread.
  2. put log4j-xx.jar under WEB-INF\lib
  3. test if log4j was loaded: add "-Dlog4j.debug" @ the end of your java options of tomcat

hope this will help

rgds

miss