sys:ls.logs 在 log4j 中是什么意思?

What does mean sys:ls.logs in log4j?

我正在阅读 log4j 文档和 logstash log4j 配置,我不知道 ${sys:ls.logs} 是什么意思。

${sys:ls.logs}是什么意思? 我如何在运行时或 OS 环境中更改此值。 我用 Ubuntu OS.

sys 是 Log4j 2 中的系统属性。Log4j 支持语法 ${prefix:name} 其中前缀标识告诉 Log4j 变量名应该在特定上下文中进行评估。查看 Log4j 2 个文档

sys : System properties. The formats are ${sys:some.property} and ${sys:some.property:-default_value}.

Property Support

You can reference properties in a configuration, Log4j will directly replace them, or Log4j will pass them to an underlying component that will dynamically resolve them. Properties come from values defined in the configuration file, system properties, environment variables, the ThreadContext Map, and data present in the event.

系统属性通常在应用程序外部定义,您可以通过 Lookup 访问它们。您可以通过添加自己的查找插件来进一步自定义 属性 提供程序。也可以使用语法 ${lookupName:key:-defaultValue}.

在 Lookup 中指定默认属性
    <Appenders>
      <File name="ApplicationLog" fileName="${sys:logPath:-/var/logs}/app.log"/>
    </Appenders>

此外,使用名为 ls.logs 的系统 属性 的查找,在 Log4j 1.x 中,语法为 ${ls.logs}。在 Log4j 2 中,语法为 ${sys:ls.logs}.

这个属性可以在应用程序源中设置或作为启动服务时的参数。例如,在 logstash 源中的 DefaultDeprecationLoggerTest for logstash 中,它被设置为 build/logs,如下所示:

    @Before
    public void setUp() throws IOException {
        System.setProperty("log4j.configurationFile", CONFIG);
        System.setProperty("ls.log.format", "plain");
        System.setProperty("ls.logs", "build/logs");

        LogTestUtils.deleteLogFile("logstash-deprecation.log");
    }