Skip to content

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 6 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
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
142 changes: 142 additions & 0 deletions docs/content/core/logger.mdx
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");
Copy link
Contributor

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

...
}
}
```
12 changes: 12 additions & 0 deletions docs/content/core/tracer.mdx
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
40 changes: 32 additions & 8 deletions docs/content/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,39 @@ Powertools is available in Maven Central. You can use your favourite dependency
...
</dependencies>
```
* [gradle](https://gradle.org/):
```
repositories {
mavenCentral()
}

dependencies {
powertools 'software.amazon.lambda:aws-lambda-powertools-java:YOUR_REQUIRED_VERSION'
}
And configure the aspectj-maven-plugin to compile-time weave (CTW) the aws-lambda-powertools-java aspects into your project:

```xml
<build>
<plugins>
...
<plugin>
<groupId>com.nickwongdev</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.12.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<complianceLevel>1.8</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>software.amazon.lambda</groupId>
<artifactId>aws-lambda-powertools-java</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
```

## Tenets
Expand Down
3 changes: 2 additions & 1 deletion docs/gatsby-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export const onRouteUpdate = ({ location, prevLocation }) => {
data: {
url: window.location.href,
section: location.pathname,
previous: prevLocation ? prevLocation.pathname : null
previous: prevLocation ? prevLocation.pathname : null,
language: 'java'
},
streamName: awsconfig.aws_kinesis_firehose_stream_name
}, 'AWSKinesisFirehose')
Expand Down
12 changes: 4 additions & 8 deletions docs/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ module.exports = {
menuTitle: 'Helpful resources',
githubRepo: 'awslabs/aws-lambda-powertools-java',
baseUrl: `${docsWebsite}`,
algoliaApiKey: 'a8491b576861e819fd50d567134eb9ce',
algoliaIndexName: 'aws-lambda-powertools-java',
logoLink: `${docsWebsite}`,
sidebarCategories: {
null: [
'index'
],
'Core utilities': [],
'Utilities': [],
'Core utilities': [
'core/tracer',
'core/logger'
]
},
navConfig: {
'Serverless Best Practices video': {
Expand All @@ -46,10 +46,6 @@ module.exports = {
}
},
footerNavConfig: {
/*'API Reference': {
href: 'https://awslabs.github.io/aws-lambda-powertools-java/api/',
target: '_blank'
},*/
Serverless: {
href: 'https://aws.amazon.com/serverless/'
},
Expand Down
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"gatsby-theme-apollo-docs/*/*/*/*/semver": "^4.3.2"
},
"keywords": [],
"license": "MIT-0",
"license": "Apache-2.0",
"repository": "https://github.com/awslabs/aws-lambda-powertools-java",
"name": "aws-lambda-powertools-java",
"description": "Powertools is a suite of utilities for AWS Lambda Functions that makes tracing with AWS X-Ray, structured logging and creating custom metrics asynchronously easier.",
Expand Down