log4j2pattern-layout

log4j2 main arguments does not get picked up by the names


I am trying to write log4j2 logs using a specific pattern:

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

I am passing a few arguments -o abc -d def -jobId 1234 -DlogFile /usr/lib/logpath.log This works for system parameters but the log prints as:

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

The code in main starts with:

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

I have tried using the argument based on the index and it works. However, the main argument lookup does not seem to work. Am I missing something here?

Maybe an off-topic question, what is the legit use-case of handling system variable over the main arguments in logging?


Solution

  • The reason for it not working is because there is a collision on the format between MainMapLookup by name and the StrSubstitutor default value delimiter.

    ${main:-jobId} gets parsed as Property main OR jobId

    This is a known bug reported here: https://issues.apache.org/jira/browse/LOG4J2-1013