Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j.properties not picked up from jar

I have a jar in the classpath of my server which contains the log4j.properties file on root of the jar for logging of the code. When the code in the jar runs the logging for the code is not occuring. When I analyzed the problem using java -Dlog4j.debug option I got to know that the log4j is loading the log4j.properties file of axis-ant.jar (this also is on my classpath) and using it instead of my jar's property file.

When i subsequently removed the axis-ant.jar from the classpath my jars log4j.properties file was picked immediately and logging for my code occured.

Can somebody please explain why is my log4j.properties fle not being loaded while axis-ant.jar is present?

Surprisingly the same jar with axis-ant.jar present in classpath is running with proper logging successfully in another server.. both the server have the same java 5 version installed and same version of log4j jar

like image 498
harsh Avatar asked Mar 23 '11 09:03

harsh


People also ask

Where is log4j properties file located?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.

Does log4j need a properties file?

It's optional if the file name is log4j. properties and it's in the project classpath. We have to configure it before using the logger. Here is a simple program showing how to configure and use log4j logging.

Will log4j properties work with log4j2?

Log4j 2 doesn't support the Log4j v1 ". properties" format anymore (yet, since v2. 4, Log4j supports a Property format, but its syntax is totally different from v1 format). New formats are XML, JSON, and YAML, see the documentation (note: if you used one of these formats in a file called ".


2 Answers

You should have a look at the log4j manual. The section "Default Initialization Procedure" describes how log4j will try to find the initialization file and explains possibilities to match a special configuration (e.g. by setting the system property log4j.configuration).

If you do not want to do any special configuration you have to ensure that your configuration file will be the first one found on the classpath.

like image 158
FrVaBe Avatar answered Oct 15 '22 23:10

FrVaBe


The properties file is picked up from the jar based on the order in which the jar is found in classpath by your application server. Now there is no way to conclusively tell the jar from which the properties file is to be loaded.

To get around this problem most projects include your log4j.properties file in the server classpath itself. This way this file is the first properties file to be loaded and this is the one which your logging code gets.

like image 36
Pushkar Avatar answered Oct 15 '22 21:10

Pushkar