简体   繁体   中英

log4j configuration file and jar application

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.

该配置文件必须位于类路径中,如果您希望应用从其他任何位置读取配置,则需要使用

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM