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条回答
男人必须洒脱
2楼-- · 2019-01-03 04:36

Try:

PropertyConfigurator.configure(getClass().getResource("/controlador/log4j.properties"));
查看更多
小情绪 Triste *
3楼-- · 2019-01-03 04:39

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

查看更多
时光不老,我们不散
4楼-- · 2019-01-03 04:39

If you have a standard Maven Project setup with Intellij, you can simply place the log4j properties file in:

project/src/main/resources

IntelliJ - log4j properties location

查看更多
Lonely孤独者°
5楼-- · 2019-01-03 04:43

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 src/main/resources/log4j.properties

查看更多
别忘想泡老子
6楼-- · 2019-01-03 04:43

I don't know this is correct way.But it solved my problem. put log4j.properties file in "project folder"/config and use PropertyConfigurator.configure("config//log4j.properties");

it will works with IDE but not when run the jar file yourself. when you run the jar file by yourself just copy the log4j.properties file in to the folder that jar file is in.when the jar and property file in same directory it runs well.

查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-03 04:46

I found that Glassfish by default is looking at [Glassfish install location]\glassfish\domains[your domain]\ as the default working directory... you can drop the log4j.properties file in this location and initialize it in your code using PropertyConfigurator as previously mentioned...

Properties props = System.getProperties();
System.out.println("Current working directory is " + props.getProperty("user.dir"));
PropertyConfigurator.configure("log4j.properties");
查看更多
登录 后发表回答