简体   繁体   中英

org.apache.log4j not able to identify my log4j.properties file

I have been trying to use log4j(or for that matter, any kind of logging library) for the first time. I am not able to make it work after almost 2 days of efforts.

Here is the log4j.properties file that I am using but it seems that the logger (called from within a junit test suite class) does not use this file at all.

private static final Logger logger = Logger.getLogger(myclass.class.getName());
...
logger.error("Message");
...

It simply types out the messages at the console(file is not created) and when I remove the appender X from rootLogger, it still spits the messages at console.

This file is kept under a package named: "resources" under the project directory in Eclipse. I have three other source packages from where I will like to use this configuration file, that's why I have made a separate package for this file.

Is it a file creation permission issue, or the parameters case is wrong(given java is strongly typed), or the location of this file?

Any help will be highly appreciated.

log4j.rootLogger=DEBUG, X, RUNLOG

log4j.appender.X=org.apache.log4j.ConsoleAppender
log4j.appender.X.layout=org.apache.log4j.PatternLayout
log4j.appender.X.layout.ConversionPattern=%m%n

log4j.appender.RUNLOG=org.apache.log4j.FileAppender
log4j.appender.RUNLOG.file=run.log
log4j.appender.RUNLOG.threshold=INFO
log4j.appender.RUNLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.RUNLOG.layout.ConversionPattern=%m%n

I don't think your resources folder is in your classpath. Try adding the folder to your classpath using Project->Properties->Java Build ->Libraries ->Add Class Folder

By default, Log4J expects to find its configuration file in the "default package": it simply looks up a resource called log4j.properties (and later log4j.xml , but that's another story) and expects it to be found in the classpath.

You mentioned that your Log4J configuration file is inside a package called resources , which is exactly why it can't find it.

You have the following options:

  • Move the log4j.properties file so it's located in the default package.
  • Create a folder in your project ( not a package), place your log4j.properties file there and add that folder to your runtime classpath.
  • Specify the log4j.configuration system property, providing it with the path to the Log4J configuration file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM