I have an AWS Lambda function written in Java. I want to run the Lambda function with a custom GraalVM runtime as is done in this blog post for instance
Whenever the application starts up, I get the following errors:
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
This is what my log4j2.xml file looks like
<Configuration status="WARN">
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1} - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Lambda"/>
</Root>
<Logger name="software.amazon.awssdk" level="WARN" />
<Logger name="software.amazon.awssdk.request" level="DEBUG" />
</Loggers>
</Configuration>
Other similar questions here suggest it is due to conflicting log4j packages in a fatjar. Based on the suggestions in this answer, I am now using shadowJar with a transformation to build the fatjar the GraalVM application is compiled from. However I am still getting the error.
The relevant parts of my gradle build file looks like this
dependencies {
implementation platform('software.amazon.awssdk:bom:2.+')
implementation platform('com.amazonaws:aws-xray-recorder-sdk-bom:2.+')
implementation 'com.amazonaws:aws-lambda-java-core:1.2.1'
implementation 'com.amazonaws:aws-lambda-java-events:2.2.9'
implementation 'com.amazonaws:aws-xray-recorder-sdk-core'
implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-core'
implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-v2'
implementation 'com.amazonaws:aws-xray-recorder-sdk-aws-sdk-v2-instrumentor'
implementation 'org.apache.logging.log4j:log4j-slf4j18-impl:2.13.2'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.formkiq:lambda-runtime-graalvm:1.1'
implementation project(":aws-java-utilities:utilities")
runtimeOnly 'com.amazonaws:aws-lambda-java-log4j2:1.2.0'
runtimeOnly 'mysql:mysql-connector-java:8.0.+'
// mqttv3 1.1.1 fails to connect for unknown reasons
testImplementation (group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: '1.1.0'){force = true}
testImplementation 'com.amazonaws:aws-iot-device-sdk-java:1.3.+'
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
testImplementation "org.mockito:mockito-core:2.+"
testImplementation project(":aws-java-utilities:end2endTest-utilities")
testImplementation 'software.amazon.awssdk:rdsdata'
}
shadowJar {
archivesBaseName = project.name + '-lambda'
manifest {
attributes 'Main-Class': 'com.formkiq.lambda.runtime.graalvm.LambdaRuntime'
}
transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer)
}
If I try and run the jar (or one build with the same task but a different main class), I get this error.
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
2021-01-16 19:35:23,290 main ERROR Error processing element Lambda ([Appenders: null]): CLASS_NOT_FOUND
2021-01-16 19:35:23,343 main ERROR Unable to locate appender "Lambda" for logger config "root"
The command I use to build the GraalVM application looks like this
docker run --rm -v $(pwd):/working oracle/graalvm-ce:20.1.0-java11 \
/bin/bash -c "
gu install native-image; \
native-image --enable-url-protocols=http,https \
-H:+AllowIncompleteClasspath \
-H:ReflectionConfigurationFiles=/working/src/main/resources/reflect.json \
-H:IncludeResourceBundles=com.mysql.cj.LocalizedErrorMessages \
-H:IncludeResources=.\*.properties \
-H:IncludeResources=.\*.xml \
-H:+ReportUnsupportedElementsAtRuntime -J-Xmx5G --no-server -jar \"/working/build/libs/$1-lambda-all.jar\";`
gradle dependencies
show me that all the references to anything with log4j are all using the same version, for instance:
...
+--- org.apache.logging.log4j:log4j-slf4j18-impl:2.13.2
| +--- org.slf4j:slf4j-api:1.8.0-beta4
| +--- org.apache.logging.log4j:log4j-api:2.13.2
| \--- org.apache.logging.log4j:log4j-core:2.13.2
| \--- org.apache.logging.log4j:log4j-api:2.13.2
...
I have seen some questions on this topic mention it is due to conflicting Log4j2Plugins.dat files. I can see in my dependencies that I have two such files.
One in com.amazonaws:aws-lambda-java-log4j2:1.2.0
and one in org.apache.logging.log4j:log4j-api:2.13.2
. According to the earlier answer I linked, the transformation in my gradle build should take care of that, but I don't know how to verify if that is true.
I have been banging my head against this problem the whole day, and I am at a real loss as to how to get the logging to work.
Any help to move me forward would be much appreciated.