log4j2主要参数不会被名称拾取

时间:2016-06-07 19:38:30

标签: log4j2 pattern-layout

我正在尝试使用特定模式编写log4j2日志:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout charset="UTF-8"
                pattern="%d{HH:mm:ss.SSS} ${main:-jobId} %-5.-5level %-25.-25c{1}%-25.-25method(%5.5line) - %msg%n" />
        </Console>
        <RollingFile name="LogFile" fileName="${sys:logFile}"
            filePattern="${sys:logFile}-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout charset="UTF-8"
                pattern="%d{yyy-MM-dd HH:mm:ss.SSS} ${main:-jobId} [%-30.-30t] %-5.-5level %-25.-25c{1}%-25.-25method(%5.5line) - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="100 MB" />
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="trace">
            <AppenderRef ref="LogFile" level="debug" />
            <AppenderRef ref="Console" level="info" />
        </Root>
    </Loggers>
</Configuration>

我传递了一些参数-o abc -d def -jobId 1234 -DlogFile /usr/lib/logpath.log这适用于系统参数,但日志打印为:

15:33:34.889 jobId INFO  Test           main                     (   74) - -b
15:33:34.890 jobId INFO  Test           main                     (   74) - -o
15:33:34.890 jobId INFO  Test           main                     (   74) - account
15:33:34.890 jobId INFO  Test           main                     (   74) - document
15:33:34.890 jobId INFO  Test           main                     (   74) - -d
15:33:34.891 jobId INFO  Test           main                     (   74) - rest
15:33:34.891 jobId INFO  Test           main                     (   74) - -l
15:33:34.891 jobId INFO  Test           main                     (   74) - 100
15:33:34.891 jobId INFO  Test           main                     (   74) - -skipmd
15:33:34.891 jobId INFO  Test           main                     (   74) - -db
15:33:34.891 jobId INFO  Test           main                     (   74) - -jobId
15:33:34.891 jobId INFO  Test           main                     (   74) - 1234

main中的代码以:

开头
public static void main(String[] args) throws Exception
    {
        MainMapLookup.setMainArguments(args);
        for (String s : args)
            logger.info(s);
...
}

我尝试过使用基于索引的参数,但它确实有用。但是,主参数查找似乎不起作用。我在这里错过了什么吗?

也许是一个偏离主题的问题,在日志记录中对主要参数处理系统变量的合法用例是什么?

1 个答案:

答案 0 :(得分:1)

它不起作用的原因是因为MainMapLookup之间的格式与StrSubstitutor默认值分隔符之间存在冲突。

$ {main:-jobId} 被解析为Property main jobId

这是此处报告的已知错误:https://issues.apache.org/jira/browse/LOG4J2-1013