log4j ver 1.x:
As others have stated, log4j looks for the first configuration file in the classpath. See: http://logging.apache.org/log4j/1.2/manual.html
But when there are both 'log4j.xml' and 'log4j.properties' files in the classpath, it seems from experimentation that log4j gives precedence to 'log4j.xml' over 'log4j.properties'.
i.e. First, log4j seems to look in the classpath for the first 'log4j.xml' file. If there is none, then log4j seems to then look in the classpath for the first 'log4j.properties' file.
Copying and pasting from the code below may help in determining what configuration file is being used:
import org.apache.log4j.Logger;
public class TestLog4j
{
static
{
System.out.println("Classpath: [" + System.getProperty( "java.class.path" ) + "]" );
System.out.println("Found logging configuration files:");
System.out.println(" log4j.xml: " + Logger.getRootLogger().getClass().getResource( "/log4j.xml" ) );
System.out.println(" log4j.properties: " + Logger.getRootLogger().getClass().getResource( "/log4j.properties" ) );
}
public static void main(String[] args)
{
System.out.println("main():");
}
}
Edit:
log4j ver 2.x:
The search order for the default configuration file is documented here: http://logging.apache.org/log4j/2.x/manual/configuration.html
i.e. For log4j version 2.x, if no higher precedence configuration file is found (e.g. log4j2-test.[properties | yaml | json | xml]) then the file log4j2.properties is used if found in the classpath. Note that log4j2.xml has the lowest precedence, and will be used only if log4j2 'properties', 'yaml' or 'json' configuration files are all not found.
Note: To aid in debugging log4j configuration issues, set the 'log4j.debug' property, e.g. with:
java -Dlog4j.debug ...
See also: How to initialize log4j properly?