使用环境变量将log4j2

使用环境变量将log4j2

本文介绍了使用环境变量将log4j2.xml文件加载到项目外部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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>

启动TomCat时出现此错误:

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:对不起,我的英语.

PD: Sorry for my english.

推荐答案

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

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>

这篇关于使用环境变量将log4j2.xml文件加载到项目外部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:35