Load log4j2.xml file outside project with enviroment variable

邮差的信 提交于 2019-12-24 05:48:43

问题


I am developing a java web-app (v3.0) for a TomCat 7.0 server and I am having troubles loading the log4j2.xml file.

I have defined the log4j2.xml file outside my project and defined the path for the file in my web.xml file.

If i hardcode the path my log4j2.xml file loads as it should.

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

On the other hand I want to use an enviroment variable to define the path.

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

When I start TomCat I have this error:

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

It looks like it isn't 'translating' the variable.

Any help will be very aprecciated.

PD: Sorry for my english.


回答1:


Log4j interpolates the value it finds for log4jConfiguration in web.xml. However, you have to use standard Log4j Lookup syntax. To get an environment variable you would specify:

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


来源:https://stackoverflow.com/questions/37189928/load-log4j2-xml-file-outside-project-with-enviroment-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!