How to specify Log4J 2.x config location?

You could use the static method #initialize(String contextName, ClassLoader loader, String configLocation) (see source here) in org.apache.logging.log4j.core.config.Configurator. (You can pass null for the class loader.)

Be aware that this class is not part of the public API so your code may break with any minor release.

For completeness, you can also specify the location of the configuration file with this system property:

-Dlog4j.configurationFile=path/to/log4j2.xml

In Windows, be aware that you need to use a URI with the log4j.configurationFile property

-Dlog4j.configurationFile=file://C:\path\to\log4j2.xml

For log4j version 2.12.1, you can find how to reconfigure log4j2 here.

Below is an example

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;

File file = new File("C:\\Path for Windows OS\\yourConfig.xml");
    
LoggerContext context = (LoggerContext) LogManager.getContext(false);
context.setConfigLocation(file.toURI());
    
Logger log  = LogManager.getLogger(YourClass.class);

It seems to me that way of configuring log4j2 is changing with new releases, so you should be aware of that.