I would like to have Log4j2 load the configuration (.properties) file I explicitly give to it. I can not find examples to do this that actually work and are for v2 and are for .properties files. It’s puzzling that with all the serious vulnerabilities that hit this module, it still insists on the philosophy of “just throw the configuration file in your classpath and we will find&load it.”. What if I have two configuration files with different extensions (xml, json, yaml, properties)? Even this Apache tutorial https://logging.apache.org/log4j/2.x/manual/configuration.html does not mention a solution to my reasonable, justified above, demand.
I have this but it works only for XML configurations (I assume as it is looking for jackson):
import java.io.File;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
public class Test {
private static final org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getLogger(Test.class.getName());
public static void main(String []args){
// from /questions/32043770/propertyconfigurator-in-log4j2
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext )LogManager.getContext(false);
File file = new File("config/log4j.properties");
// this will force a reconfiguration
context.setConfigLocation(file.toURI());
}
}
Can someone please give me an example on how to create a static logger loading its config from specific .properties file?