在log4j xml配置中使用系统环境变量。

时间:2022-06-13 10:56:43

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参数传递很多东西。

5 个解决方案

#1


13  

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中记录。所以要确保你使用的是正确的版本。它对1不起作用。X版本。

    <Appenders>
    <File name="file" fileName="${env:LOG_PATH}">
        <PatternLayout>
            <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
        </PatternLayout>
    </File>
</Appenders>

#2


46  

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

#3


11  

I think this is not supported, but basically you can do two things to bring in your environment variables:

我认为这是不支持的,但是基本上你可以做两件事来引入你的环境变量:

  1. Use System.setProperty before Log4J gets configured

    使用系统。在配置Log4J之前设置setProperty

  2. Convert (your) environment variables to system properties in your launcher

    将(您的)环境变量转换为启动程序中的系统属性

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或类似。

#4


2  

You need to put a colon between env and the name of the variable, like this:

需要在env和变量名之间加一个冒号,如下所示:

<level value="${env:LOG_LEVEL}" />

#5


0  

Create a system variable. I prefer to use setenv.bat for such variables.

创建一个系统变量。我更喜欢使用setenv。蝙蝠等变量。

@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>

#1


13  

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中记录。所以要确保你使用的是正确的版本。它对1不起作用。X版本。

    <Appenders>
    <File name="file" fileName="${env:LOG_PATH}">
        <PatternLayout>
            <Pattern>%d %p %c{1.} [%t] %m %ex%n</Pattern>
        </PatternLayout>
    </File>
</Appenders>

#2


46  

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

#3


11  

I think this is not supported, but basically you can do two things to bring in your environment variables:

我认为这是不支持的,但是基本上你可以做两件事来引入你的环境变量:

  1. Use System.setProperty before Log4J gets configured

    使用系统。在配置Log4J之前设置setProperty

  2. Convert (your) environment variables to system properties in your launcher

    将(您的)环境变量转换为启动程序中的系统属性

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或类似。

#4


2  

You need to put a colon between env and the name of the variable, like this:

需要在env和变量名之间加一个冒号,如下所示:

<level value="${env:LOG_LEVEL}" />

#5


0  

Create a system variable. I prefer to use setenv.bat for such variables.

创建一个系统变量。我更喜欢使用setenv。蝙蝠等变量。

@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>