Java Log4J2 属性替换 - 默认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22028399/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:08:37  来源:igfitidea点击:

Log4J2 property substitution - default

javalogginglog4jlog4j2

提问by Leos Literak

I just wonder if there is any way to provide default value for property substitution in LOG4J?

我只是想知道是否有任何方法可以为 LOG4J 中的属性替换提供默认值?

I want to pass file path in java system property and then use it with "${env:mySystemProperty}". But what if developer forgets to set this property? Then I would like to have some meaningful default value defined in log4j2.xml.

我想在 java 系统属性中传递文件路径,然后将它与“${env:mySystemProperty}”一起使用。但是如果开发者忘记设置这个属性怎么办?然后我想在 log4j2.xml 中定义一些有意义的默认值。

Any idea how to achieve this functionality?

知道如何实现此功能吗?

EDIT:

编辑:

The env substitution does not work for me:

env 替换对我不起作用:

standalone.conf

独立配置文件

-DoauthLoginLogPath=/path/oauth2.log

log4j2.xml

log4j2.xml

<Appender type="File" name="File" fileName="${env:oauthLoginLogPath}" immediateFlush="true">
<Appender type="File" name="File" fileName="${sys:oauthLoginLogPath}" immediateFlush="true">

I can see in wildfly console the property, I restarted server but I cannot get it done.

我可以在 Wildfly 控制台中看到该属性,我重新启动了服务器,但无法完成。

采纳答案by Remko Popma

Default Property map

默认属性映射

Looking at http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitutionyou can specify a default property map in the configuration file. That takes this form:

查看http://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution,您可以在配置文件中指定默认属性映射。采取这种形式:

<Configuration status="debug">
  <Properties>
    <Property name="oauthLoginLogPath">default/location/of/oauth2.log</Property>
  </Properties>
  ...
  <Appenders>
    <Appender type="File" name="File" fileName="${sys:oauthLoginLogPath}">
    ....
</Configuration

Then, if you start your app with system property -DoauthLoginLogPath=/path/oauth2.log, the File appender fileNamevalue will first be looked up in system properties, but if that fails, it will fall back to the property defined in the Propertiessection at the top of the log4j2.xml configuration file.

然后,如果您使用 system property 启动您的应用程序,将首先在系统属性中查找-DoauthLoginLogPath=/path/oauth2.logFile appenderfileName值,但如果失败,它将回Properties退到 log4j2.xml 配置文件顶部部分中定义的属性.

Inline

排队

A second way is to provide the default value inline:

第二种方法是提供内联默认值:

<Appender type="File" name="File" fileName="${sys:oauthLoginLogPath:-default/location/of/oauth2.log}">

Generally all Log4j2 lookups follow this pattern: ${type:key:-defaultValue}.

通常所有 Log4j2 查找都遵循以下模式:${type:key:-defaultValue}.

Env vs sys

环境与系统

By the way, the envprefix is for environment variables (like %PATH% on Windows), and is not related to sys, which is Java system properties. See also http://logging.apache.org/log4j/2.x/manual/lookups.html

顺便说一句,env前缀用于环境变量(如 Windows 上的 %PATH%),与sysJava 系统属性无关。另见http://logging.apache.org/log4j/2.x/manual/lookups.html

回答by GoGoris

You can use the same ${sys:propName:-default}syntax. Notice the ':-', it is is called "variable default value delimiter". The defaultvalue for the "variable default value delimiter" is :-, as in bashand other *nixshells.

您可以使用相同的${sys:propName:-default}语法。注意':-',它被称为“变量默认值分隔符”。在默认的“价值变量默认值分隔符”是:-,在庆典和其他的* nix炮弹。

You can read more about this in the Log4j 2 documentation for the StrSubstitutorclass.

您可以在StrSubstitutor类的 Log4j 2 文档中阅读有关此内容的更多信息。

To use the same example:

使用相同的示例:

<Configuration status="debug">
  ...
    <Appenders>
        <Appender type="File" name="File"
                  fileName="${sys:oauthLoginLogPath:-default/location/of/oauth2.log}">
        ....
    </Appenders>
</Configuration>