Java 在log4j xml配置中使用系统环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/201188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using system environment variables in log4j xml configuration
提问by Derek Lewis
Is it possible to reference system environment variables (as opposed to Java system properties) in a log4j xml configuration file?
是否可以在 log4j xml 配置文件中引用系统环境变量(而不是 Java 系统属性)?
I'd like to be able to do something like:
我希望能够执行以下操作:
<level value="${env.LOG_LEVEL}" />
and have it get that from the system environment variables, so I can avoid having to pass in so many things with -D parameters.
并让它从系统环境变量中获取它,这样我就可以避免使用 -D 参数传递这么多东西。
采纳答案by Shoham
This syntax is documented only in log4j 2.X so make sure you are using the correct version. It does not work on the 1.X versions.
此语法仅记录在 log4j 2.X 中,因此请确保您使用的是正确的版本。它不适用于 1.X 版本。
<Appenders>
<File name="file" fileName="${env:LOG_PATH}">
<PatternLayout>
<Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
</PatternLayout>
</File>
</Appenders>
回答by Einar
I tried to do that recently and couldn't get it to work. What I ended up doing is sending a variable at startup. So say you have an environment variable called $LOG_LEVEL:
我最近尝试这样做,但无法正常工作。我最终做的是在启动时发送一个变量。假设您有一个名为 $LOG_LEVEL 的环境变量:
<level value="${log_level}" />
and at startup...
并在启动时...
java -Dlog_level=$LOG_LEVEL your_app
回答by Martin Probst
I think this is not supported, but basically you can do two things to bring in your environment variables:
我认为这是不支持的,但基本上你可以做两件事来引入你的环境变量:
Use System.setProperty before Log4J gets configured
Convert (your) environment variables to system properties in your launcher
在配置 Log4J 之前使用 System.setProperty
将(您的)环境变量转换为启动器中的系统属性
The first option basically boils down to this:
第一个选项基本上归结为:
for (Map<String,String>.Entry entry : System.getenv().entrySet()) {
System.setProperty(entry.getKey(), entry.getValue());
}
... but the question is of course where to put this code. In particular if you're running within some sort of Tomcat container or similar, this might be troublesome.
...但问题当然是把这段代码放在哪里。特别是如果您在某种 Tomcat 容器或类似容器中运行,这可能会很麻烦。
The other largely depends on your environment. Basically if you have a shell script that starts your app, you can write some shell magic to set all environment variables as properties, or just the ones you need, e.g.:
另一个很大程度上取决于您的环境。基本上,如果你有一个 shell 脚本来启动你的应用程序,你可以编写一些 shell 魔法来将所有环境变量设置为属性,或者只是你需要的那些,例如:
java -DMY_ENV=$MY_ENV -DMY_OTHER_ENV=$MY_OTHER_ENV -cp ... com.example.Main
It's also possible to alter your server startup scripts to support this, e.g. catalina.sh or similar.
也可以更改您的服务器启动脚本以支持此功能,例如 catalina.sh 或类似的。
回答by Daniel Estrada
You need to put a colon between env and the name of the variable, like this:
您需要在 env 和变量名称之间放置一个冒号,如下所示:
<level value="${env:LOG_LEVEL}" />
回答by tkolleh
Create a system variable. I prefer to use setenv.bat for such variables.
创建系统变量。我更喜欢将 setenv.bat 用于此类变量。
@echo off
rem app specific log dir
set "APP_LOG_ROOTDIR=../app/app-log"
exit /b 0
Add reference in log4j.xml file
在 log4j.xml 文件中添加引用
<appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="DEBUG" />
<param name="MaxFileSize" value="512KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="${APP_LOG_ROOTDIR}/app.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1} %m %n" />
</layout>
</appender>