From a6f5115854230c9b6891953a1180cda74c634c4e Mon Sep 17 00:00:00 2001 From: Daniel Torok Date: Wed, 8 Nov 2023 16:35:05 +0000 Subject: [PATCH] Updated readme for log4j2 (#46) (#460) Co-authored-by: Daniel Torok --- aws-lambda-java-log4j2/README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/aws-lambda-java-log4j2/README.md b/aws-lambda-java-log4j2/README.md index 6f792131..b1b739b6 100644 --- a/aws-lambda-java-log4j2/README.md +++ b/aws-lambda-java-log4j2/README.md @@ -22,6 +22,11 @@ Example for Maven pom.xml log4j-api 2.17.1 + + org.apache.logging.log4j + log4j-layout-template-json + 2.17.1 + .... ``` @@ -108,7 +113,7 @@ Add the following file `/src/main/resources/log4j2.xml` - + @@ -124,6 +129,8 @@ package example; import com.amazonaws.services.lambda.runtime.Context; +import static org.apache.logging.log4j.CloseableThreadContext.put; +import org.apache.logging.log4j.CloseableThreadContext.Instance; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -143,6 +150,12 @@ public class Hello { logger.error("log data from log4j err. \n this is a continuation of log4j.err"); + // When logging in JSON, you can also add custom fields + // In java11+ you can use the `try (var ctx = put("name", name)) {}` structure + Instance ctx = put("name", name); + logger.info("log line with input name"); + ctx.close(); + // Return will include the log stream name so you can look // up the log later. return String.format("Hello %s. log stream = %s", name, context.getLogStreamName());