Skip to content

Commit

Permalink
Updated readme for log4j2 (#46) (#460)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Torok <torokd@amazon.com>
  • Loading branch information
dtorok and dtorok authored Nov 8, 2023
1 parent 5a94e19 commit a6f5115
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion aws-lambda-java-log4j2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Example for Maven pom.xml
<artifactId>log4j-api</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-layout-template-json</artifactId>
<version>2.17.1</version>
</dependency>
....
</dependencies>
```
Expand Down Expand Up @@ -108,7 +113,7 @@ Add the following file `<project-dir>/src/main/resources/log4j2.xml`
</Lambda>
</Appenders>
<Loggers>
<Root level="info">
<Root level="${env:AWS_LAMBDA_LOG_LEVEL:-INFO}">
<AppenderRef ref="Lambda" />
</Root>
</Loggers>
Expand All @@ -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;

Expand All @@ -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());
Expand Down

0 comments on commit a6f5115

Please sign in to comment.