小编典典

使用环境变量将log4j2.xml文件加载到项目外部

tomcat

我正在为TomCat 7.0服务器开发Java Web应用程序(v3.0),但在加载log4j2.xml文件时遇到了麻烦。

我已经在log4j2.xml项目外部定义了文件,并在web.xml文件中定义了文件的路径。

如果我对路径进行硬编码,则log4j2.xml文件将按原样加载。

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>file:///C:/my/path/log4j2.xml</param-value>
</context-param>

另一方面,我想使用一个环境变量来定义路径。

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>file:///${ENVIROMENT_VARIABLE}/log4j2.xml</param-value>
</context-param>

当我启动TomCat时,出现以下错误:

ERROR SatusLogger Unable to access file:///$%7BENVIROMENT_VARIABLE%7D/log4j2.xml

看起来它不是在“翻译”变量。

任何帮助将不胜感激。

PD:对不起,我的英语。


阅读 1095

收藏
2020-06-16

共1个答案

小编典典

Log4j内插它在web.xml中为log4jConfiguration找到的值。但是,您必须使用标准的Log4j查找语法。要获取环境变量,您可以指定:

<context-param>
    <param-name>log4jConfiguration</param-name>
    <param-value>file:///${env:ENVIROMENT_VARIABLE}/log4j2.xml</param-value>
</context-param>
2020-06-16