简体   繁体   English

如何在Web应用程序外部定义任意文件以配置log4j2

[英]How can I define an arbitrary file outside my web application to configure log4j2

My web application will be deployed to Weblogic application servers on Windows and Linux/Unix in different environments. 我的Web应用程序将部署到Windows和Linux / Unix上的Weblogic应用程序服务器中的不同环境中。 The log file location, appenders and log levels will vary between the different deployments and we would like to be able to change the logging configuration during runtime (by exchanging the config file), so I cannot embed a log4j2.xml (or whatever other config file) into my deployment. 日志文件位置,appenders和日志级别在不同的部署之间会有所不同,我们希望能够在运行时更改日志配置(通过交换配置文件),因此我无法嵌入log4j2.xml(或其他任何配置)进入我的部署。 And since I'm running on Application servers I cannot control, I've got no chance to add environment variables to point to another configuration Location. 由于我在应用程序服务器上运行,我无法控制,我没有机会添加环境变量来指向另一个配置位置。

Currently, my log4j2.xml resides in the classpath of my application and is being packaged into my war file. 目前,我的log4j2.xml位于我的应用程序的类路径中,并被打包到我的war文件中。 Is there any way to tell Log4J2 to use a configuration file eg relative to the application root (like Log4J's configureAndWatch(fileLocation) method)? 有没有办法告诉Log4J2使用配置文件,例如相对于应用程序根目录(如Log4J的configureAndWatch(fileLocation)方法)?

I found lots of examples of how to configure Log4J2, but everything I found about the config file location points to the applications class path. 我找到了很多关于如何配置Log4J2的示例,但是我发现的有关配置文件位置的所有内容都指向了应用程序类路径。

I finally found a solution for my problem. 我终于找到了解决问题的方法。 I added a file named 我添加了一个名为的文件

log4j2.component.properties

to my project (in src/main/resources). 到我的项目(在src / main / resources中)。 This file contains a property pointing to the location of my log4j2 configuration file: 此文件包含指向log4j2配置文件位置的属性:

log4j.configurationFile=.//path//on//my//application//server//someLog4j2ConfigFile.xml

This causes log4j2 to read that file and configure itself from it's content. 这会导致log4j2读取该文件并根据其内容进行自我配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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