I want the log produced by the java runtime in an AWS lambda function to be in JSON format.
I’m talking about logs like these:
2022-07-27T19:30:21.094+03:00 START RequestId: 09e6e95c-5b1f-53c0-8483-b207312166be Version: $LATEST
2022-07-27T19:30:37.640+03:00 SLF4J: Class path contains multiple SLF4J bindings.
2022-07-27T19:31:03.536+03:00 END RequestId: 09e6e95c-5b1f-53c0-8483-b207312166be
2022-07-27T19:31:03.536+03:00 REPORT RequestId: 09e6e95c-5b1f-53c0-8483-b207312166be Duration: 42441.58 ms Billed Duration: 42442 ms Memory Size: 512 MB Max Memory Used: 226 MB
2
Answers
You cannot alter the format of the START, END, or REPORT log lines. These are produced by the Lambda service.
In AWS Lambda, when you write a Java Lambda function, you have two options for logging.
log4j
LambdaLogger
https://docs.aws.amazon.com/lambda/latest/dg/java-logging.html#java-wt-logging
Add log4j2 propety in your lambda class path
log4j2.properties file to use the JSON Template Layout: