javaamazon-web-servicesaws-lambdaslf4j

Unrecognised conversion specifier [msg] in Log4j2 pattern layout on AWS Lambda but works locally


I'm using Log4j2 in an AWS Lambda project, and everything works fine when running locally. However, when deploying to Lambda and logging to CloudWatch, I receive the following error:

Here’s the pattern layout in my log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Lambda name="Lambda" format="${env:AWS_LAMBDA_LOG_FORMAT:-TEXT}">
            <LambdaTextFormat>
                <PatternLayout pattern="%d{ISO8601} [%t] %-5level %logger - %msg%n"/>
            </LambdaTextFormat>
            <LambdaJSONFormat>
                <JsonTemplateLayout eventTemplateUri="classpath:logback-config.json" />
            </LambdaJSONFormat>
        </Lambda>
    </Appenders>
    <Loggers>
        <Root level="${env:AWS_LAMBDA_LOG_LEVEL:-INFO}">
            <AppenderRef ref="Lambda" />
        </Root>
    </Loggers>
</Configuration>

and build.gradle dependencies

    implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
    implementation 'com.amazonaws:aws-lambda-java-events:3.11.0'
    implementation 'com.amazonaws:aws-lambda-java-log4j2:1.6.0'
        
    implementation 'software.amazon.awssdk:s3:2.20.112'
    implementation 'software.amazon.awssdk:core:2.20.0'
    implementation 'software.amazon.awssdk:ssm:2.20.0' 
    implementation 'software.amazon.awssdk:lambda:2.20.0'

    implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.24.3'
    implementation 'org.apache.logging.log4j:log4j-core:2.24.3'
    implementation 'org.apache.logging.log4j:log4j-api:2.19.0'
    implementation 'org.apache.logging.log4j:log4j-layout-template-json:2.24.3'

and the error

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|   timestamp   |                                                                             message                                                                              |
|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1741259442561 | INIT_START Runtime Version: java:17.v39 Runtime Version ARN: arn:aws:lambda:us-east-1::runtime:6f7d8c19e520ca92114d1fccd25e3ede3da8a01faa3fcd6e6a1218b56d944593  |
| 1741259443119 | 2025-03-06T11:10:43.113934328Z main ERROR Unrecognized format specifier [d]                                                                                      |
| 1741259443120 | 2025-03-06T11:10:43.120584259Z main ERROR Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.                                   |
| 1741259443124 | 2025-03-06T11:10:43.124700878Z main ERROR Unrecognized format specifier [thread]                                                                                 |
| 1741259443125 | 2025-03-06T11:10:43.124934783Z main ERROR Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.                              |
| 1741259443125 | 2025-03-06T11:10:43.125096532Z main ERROR Unrecognized format specifier [level]                                                                                  |
| 1741259443125 | 2025-03-06T11:10:43.125206983Z main ERROR Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.                               |
| 1741259443125 | 2025-03-06T11:10:43.125370997Z main ERROR Unrecognized format specifier [logger]                                                                                 |
| 1741259443125 | 2025-03-06T11:10:43.125476937Z main ERROR Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.                              |
| 1741259443125 | 2025-03-06T11:10:43.125588790Z main ERROR Unrecognized format specifier [msg]                                                                                    |
| 1741259443125 | 2025-03-06T11:10:43.125706041Z main ERROR Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.                                 |
| 1741259443125 | 2025-03-06T11:10:43.125833587Z main ERROR Unrecognized format specifier [n]                                                                                      |
| 1741259443125 | 2025-03-06T11:10:43.125927525Z main ERROR Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.                                   |
| 1741259443156 | 2025-03-06T11:10:43.156795171Z main ERROR Unrecognized format specifier [d]                                                                                      |
| 1741259443157 | 2025-03-06T11:10:43.156992848Z main ERROR Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.                                   |
| 1741259443157 | 2025-03-06T11:10:43.157109763Z main ERROR Unrecognized format specifier [thread]                                                                                 |
| 1741259443157 | 2025-03-06T11:10:43.157196547Z main ERROR Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.                              |
| 1741259443157 | 2025-03-06T11:10:43.157292534Z main ERROR Unrecognized format specifier [level]                                                                                  |
| 1741259443157 | 2025-03-06T11:10:43.157385179Z main ERROR Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.                               |
| 1741259443157 | 2025-03-06T11:10:43.157515572Z main ERROR Unrecognized format specifier [logger]                                                                                 |
| 1741259443157 | 2025-03-06T11:10:43.157599552Z main ERROR Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.                              |
| 1741259443157 | 2025-03-06T11:10:43.157691427Z main ERROR Unrecognized format specifier [msg]                                                                                    |
| 1741259443157 | 2025-03-06T11:10:43.157779528Z main ERROR Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.                                 |
| 1741259443157 | 2025-03-06T11:10:43.157864428Z main ERROR Unrecognized format specifier [n]                                                                                      |
| 1741259443157 | 2025-03-06T11:10:43.157940035Z main ERROR Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.                                   |
| 1741259443315 | START RequestId: b003f85b-8b31-4e70-9628-dd03607219da Version: $LATEST                                                                                           |
| 1741259443700 | %d [%thread] %-5level %logger - %msg%n2025-03-06T11:10:43.700301498Z main ERROR Unrecognized format specifier [d]                                                |
| 1741259443700 | 2025-03-06T11:10:43.700471652Z main ERROR Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.                                   |
| 1741259443700 | 2025-03-06T11:10:43.700581914Z main ERROR Unrecognized format specifier [thread]                                                                                 |
| 1741259443700 | 2025-03-06T11:10:43.700684369Z main ERROR Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.                              |
| 1741259443700 | 2025-03-06T11:10:43.700787373Z main ERROR Unrecognized format specifier [level]                                                                                  |
| 1741259443700 | 2025-03-06T11:10:43.700868065Z main ERROR Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.                               |
| 1741259443701 | 2025-03-06T11:10:43.700988647Z main ERROR Unrecognized format specifier [logger]                                                                                 |
| 1741259443701 | 2025-03-06T11:10:43.701071281Z main ERROR Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.                              |
| 1741259443701 | 2025-03-06T11:10:43.701182242Z main ERROR Unrecognized format specifier [msg]                                                                                    |
| 1741259443701 | 2025-03-06T11:10:43.701262817Z main ERROR Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.                                 |
| 1741259443701 | 2025-03-06T11:10:43.701354991Z main ERROR Unrecognized format specifier [n]                                                                                      |
| 1741259443701 | 2025-03-06T11:10:43.701434557Z main ERROR Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.                                   |
| 1741259443702 | 2025-03-06T11:10:43.702174857Z main ERROR Unrecognized format specifier [d]                                                                                      |
| 1741259443702 | 2025-03-06T11:10:43.702287908Z main ERROR Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.                                   |
| 1741259443702 | 2025-03-06T11:10:43.702385288Z main ERROR Unrecognized format specifier [thread]                                                                                 |
| 1741259443702 | 2025-03-06T11:10:43.702463104Z main ERROR Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.                              |
| 1741259443702 | 2025-03-06T11:10:43.702559185Z main ERROR Unrecognized format specifier [level]                                                                                  |
| 1741259443702 | 2025-03-06T11:10:43.702640991Z main ERROR Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.                               |
| 1741259443702 | 2025-03-06T11:10:43.702736245Z main ERROR Unrecognized format specifier [logger]                                                                                 |
| 1741259443702 | 2025-03-06T11:10:43.702814560Z main ERROR Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.                              |
| 1741259443702 | 2025-03-06T11:10:43.702905452Z main ERROR Unrecognized format specifier [msg]                                                                                    |
| 1741259443703 | 2025-03-06T11:10:43.702981466Z main ERROR Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.                                 |
| 1741259443703 | 2025-03-06T11:10:43.703065460Z main ERROR Unrecognized format specifier [n]                                                                                      |
| 1741259443703 | 2025-03-06T11:10:43.703142907Z main ERROR Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.                                   |
| 1741259458361 | %d [%thread] %-5level %logger - %msg%n%d [%thread] %-5level %logger - %msg%n                                                                                     |
| 1741259463396 | %d [%thread] %-5level %logger - %msg%n%d [%thread] %-5level %logger - %msg%n                                                                                     |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

What I’ve tried:

I tested with a simplified pattern (without %msg) and it works fine. I ensured that the dependencies are packaged correctly for Lambda. I’ve updated all dependencies to the latest versions. I’ve also tried testing with %logger and %t specifiers, which work fine locally but not in the lambda.

Question: What could be causing this issue with the %msg specifier when running on AWS Lambda, and how can I resolve it? Is there any Lambda-specific configuration I need to adjust?


Solution

  • A transformer should be added to gradle

    shadowJar {
        transform(Log4j2PluginsCacheFileTransformer)
    }