Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated readme for log4j2 (#46) #460

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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