Log4J properties file not found
I have a Java Project that was added to the Java Build Path of a Web Project.
On the first Java project I added the Log4J JAR file to the Java Build Path and since this project was added to the Java Build Project of the Web Project, the JAR file was automatically added to the Web Project Java Build Path also.
On the Web Project I have a Web Service that instantiates a class of the Java Project. That class has a simple Logger, and it works correctly.
Now, I am trying to create a properties file named log4j.properties to configure the Logger, Appender and LayoutPattern.
Whenever I try to call a method of the instantiaded class I get this error on the console:
log4j:ERROR Could not read configuration file [log4j.properties].
What am I doing wrong?
Here's the log4j properties file:
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.a开发者_如何学运维ppender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Sorry but it was a false alarm...
Like I said, the project where the class that instantiates the logger resides is added as a dependency o a main project where the web services are defined.
As a result that project is published on a JAR file and with the suposed solution I mentioned:
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
I get a path like:
C:/project_path.jar/log4j.properties.
Obviously the propertied files still isn't found...
Sory... Still working on a solution
If using log4j 2.x, you need to have a file called log4j2.xml
.
log4j.properties
won't do it.
Place your log4j.properties
file in your classes
directory if using unpacked WAR, else place it in the src
folder (root folder for your java classes).
The only way I found to solve this was to put the log4j.properties file in the src root folder.
Then on the class that instantiates the logger the following line:
PropertyConfigurator.configure("log4j.properties")
... had to be changed to:
PropertyConfigurator.configure(getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "log4j.properties");
And finally the file was found.
Thanks for the insight Pål
Ok, sometimes the obvious answer is the one you least expect.
As it turned out I simply needed to remove the PropertyConfigurator.configure(xxx) line and place the log4j.properties file on the src folder of the dependency project.
Thanks
精彩评论