-
Notifications
You must be signed in to change notification settings - Fork 90
docs: Implement Logger and Tracer part #27
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f608fe
docs: Implement Logger and Tracer part
stevehouel 4f59196
docs: fix logger documentation
stevehouel aba126a
Merge branch 'master' into automation
stevehouel a653aee
Merge branch 'master' into automation
stevehouel d267bd8
docs: fixing documentation
stevehouel b3d6822
Merge branch 'master' into automation
stevehouel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
--- | ||
title: Logger | ||
description: Core utility | ||
--- | ||
|
||
import Note from "../../src/components/Note" | ||
|
||
Logger provides an opinionated logger with output structured as JSON. | ||
|
||
**Key features** | ||
|
||
* Capture key fields from Lambda context, cold start and structures logging output as JSON | ||
* Log Lambda event when instructed (disabled by default) | ||
- Enable explicitly via annotation param | ||
* Append additional keys to structured log at any point in time | ||
|
||
## Initialization | ||
|
||
Powertools extends the functionality of Log4J. Below is an example log4j2.xml file, with the LambdaJsonLayout configured. | ||
|
||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2"> | ||
<Appenders> | ||
<Console name="JsonAppender" target="SYSTEM_OUT"> | ||
<LambdaJsonLayout compact="true" eventEol="true"/> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Logger name="JsonLogger" level="INFO" additivity="false"> | ||
<AppenderRef ref="JsonAppender"/> | ||
</Logger> | ||
<Root level="info"> | ||
<AppenderRef ref="JsonAppender"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> | ||
``` | ||
|
||
You can also explicitly set a service name via `POWERTOOLS_SERVICE_NAME` env var. This sets **service** key that will be present across all log statements. | ||
|
||
## Standard structured keys | ||
|
||
Your Logger will always include the following keys to your structured logging: | ||
|
||
Key | Type | Example | Description | ||
------------------------------------------------- | ------------------------------------------------- | --------------------------------------------------------------------------------- | ------------------------------------------------- | ||
**timestamp** | String | "2020-05-24 18:17:33,774" | Timestamp of actual log statement | ||
**level** | String | "INFO" | Logging level | ||
**coldStart** | Boolean | true| ColdStart value. | ||
**service** | String | "payment" | Service name defined. "service_undefined" will be used if unknown | ||
**message** | String | "Collecting payment" | Log statement value. Unserializable JSON values will be casted to string | ||
**functionName**| String | "example-powertools-HelloWorldFunction-1P1Z6B39FLU73" | ||
**functionVersion**| String | "12" | ||
**functionMemorySize**| String | "128" | ||
**functionArn**| String | "arn:aws:lambda:eu-west-1:012345678910:function:example-powertools-HelloWorldFunction-1P1Z6B39FLU73" | ||
|
||
|
||
## Capturing context Lambda info | ||
|
||
You can enrich your structured logs with key Lambda context information via `logEvent` annotation parameter. | ||
|
||
```java:title=App.java | ||
package helloworld; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import software.amazon.lambda.logging.PowerLogger; | ||
import software.amazon.lambda.logging.PowerToolsLogging; | ||
... | ||
|
||
/** | ||
* Handler for requests to Lambda function. | ||
*/ | ||
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
|
||
Logger log = LogManager.getLogger(); | ||
|
||
@PowerToolsLogging | ||
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { | ||
... | ||
} | ||
} | ||
``` | ||
|
||
You can also explicitly log any incoming event using `logEvent` param. | ||
|
||
<Note type="warning"> | ||
This is disabled by default to prevent sensitive info being logged. | ||
</Note><br/> | ||
|
||
```java:title=App.java | ||
package helloworld; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import software.amazon.lambda.logging.PowerLogger; | ||
import software.amazon.lambda.logging.PowerToolsLogging; | ||
... | ||
|
||
/** | ||
* Handler for requests to Lambda function. | ||
*/ | ||
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
|
||
Logger log = LogManager.getLogger(); | ||
|
||
@PowerToolsLogging(logEvent = true) | ||
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { | ||
... | ||
} | ||
} | ||
``` | ||
|
||
## Appending additional keys | ||
|
||
You can append your own keys to your existing Logger via `customKey`. | ||
|
||
```java:title=App.java | ||
package helloworld; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import software.amazon.lambda.logging.PowerLogger; | ||
import software.amazon.lambda.logging.PowerToolsLogging; | ||
... | ||
|
||
/** | ||
* Handler for requests to Lambda function. | ||
*/ | ||
public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
|
||
Logger log = LogManager.getLogger(); | ||
|
||
@PowerToolsLogging(logEvent = true) | ||
public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { | ||
... | ||
PowerLogger.customKey("test", "willBeLogged"); | ||
... | ||
} | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: Tracer | ||
description: Core utility | ||
--- | ||
|
||
import Note from "../../src/components/Note" | ||
|
||
Tracer is an opinionated thin wrapper for [AWS X-Ray Java SDK](https://github.com/aws/aws-xray-sdk-java/). | ||
|
||
**Key features** | ||
|
||
* Support tracing async methods |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will probably rename this api based on suggestion from heitor to make it explicit that it will append to the log. Just a note