Where should I put the log4j.properties file?

2019-01-03 03:56发布

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

17条回答
\"骚年 ilove
2楼-- · 2019-01-03 04:47

Actually, I've just experienced this problem in a stardard Java project structure as follows:

\myproject
    \src
    \libs
    \res\log4j.properties

In Eclipse I need to add the res folder to build path, however, in Intellij, I need to mark the res folder as resouces as the linked screenshot shows: right click on the res folder and mark as resources.

查看更多
We Are One
3楼-- · 2019-01-03 04:50

Put log4j.properties in classpath. Here is the 2 cases that will help you to identify the proper location- 1. For web application the classpath is /WEB-INF/classes.

\WEB-INF      
    classes\
        log4j.properties
  1. To test from main / unit test the classpath is source directory

    \Project\
       src\
          log4j.properties
    
查看更多
Melony?
4楼-- · 2019-01-03 04:52

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

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-03 04:54

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.

查看更多
贼婆χ
6楼-- · 2019-01-03 04:54

I've spent a great deal of time to figure out why the log4j.properties file is not seen.
Then I noticed it was visible for the project only when it was in both MyProject/target/classes/ and MyProject/src/main/resources folders.
Hope it'll be useful to somebody.
PS: The project was maven-based.

查看更多
Animai°情兽
7楼-- · 2019-01-03 04:56

For a Maven Based Project keep your log4j.properties in src/main/resources. Nothing else to do!

查看更多
登录 后发表回答