Load log4j2.xml file outside project with envirome

2019-07-27 21:01发布

问题:

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>