1
votes

I'm using the log4j2 library to manage the logging process. I created a configuration file named log4j2.xml containing the Appenders and Loggers configurations. Then, I defined a Logger in each class

private static Logger my_logger = LogManager.getLogger(my_class);

I did not specify anywhere the name of the conf file, so I think that the library implicitly get and read it.

Now, I need to provide my application in the form of a jar file, so I need to make the config file available so that the user can modify and configure it.

In my case, I suggest to create a XXX folder at the level of the jar file, containing all the configuration files used by my app.

My question is how can I say to the app "get XXX/log4j2.xml" rather than the xml contained into the jar.

3
logging.apache.org/log4j/2.x/manual/configuration.html explains how Log4j2 looks for configuration file and how you can override it from the command line. - Oleg Estekhin

3 Answers

1
votes

that config file must be located in the class path, if you want the app to read the configuration from any other location then you need to specify that using

PropertyConfigurator.configure("/myPath/log4j.properties");
0
votes

Make any folder and put your property or xml file in that. In order to read the property file you can do something like this:

Properties objProperties = new Properties();   
 <your-class>.class.getClassLoader().getResource("folder/log4j.properties");
objProperties.load(isFile);

or, Also this:

InputStream ist = Thread.currentThread().getContextClassLoader().getResourceAsStream("folder/log4j.properties");

In case of java web application please use the link

-1
votes

I had a similar task a few weeks ago. I solved it this way:

  • Store a template of your log4j2.xml inside your jar files resource folder

  • When running your application, check for a file named log4j2.xml in the jar files current directory

  • If there is one, use that to create your logger

  • If not, copy your template from within your jar to the jar files directory and then use that to create your logger.

Cheers