|
| 1 | +--- |
| 2 | +title: Logger |
| 3 | +description: Core utility |
| 4 | +--- |
| 5 | + |
| 6 | +import Note from "../../src/components/Note" |
| 7 | + |
| 8 | +Logger provides an opinionated logger with output structured as JSON. |
| 9 | + |
| 10 | +**Key features** |
| 11 | + |
| 12 | +* Capture key fields from Lambda context, cold start and structures logging output as JSON |
| 13 | +* Log Lambda event when instructed (disabled by default) |
| 14 | + - Enable explicitly via annotation param |
| 15 | +* Append additional keys to structured log at any point in time |
| 16 | + |
| 17 | +## Initialization |
| 18 | + |
| 19 | +Powertools extends the functionality of Log4J. Below is an example log4j2.xml file, with the LambdaJsonLayout configured. |
| 20 | + |
| 21 | +```xml |
| 22 | +<?xml version="1.0" encoding="UTF-8"?> |
| 23 | +<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2"> |
| 24 | + <Appenders> |
| 25 | + <Console name="JsonAppender" target="SYSTEM_OUT"> |
| 26 | + <LambdaJsonLayout compact="true" eventEol="true"/> |
| 27 | + </Console> |
| 28 | + </Appenders> |
| 29 | + <Loggers> |
| 30 | + <Logger name="JsonLogger" level="INFO" additivity="false"> |
| 31 | + <AppenderRef ref="JsonAppender"/> |
| 32 | + </Logger> |
| 33 | + <Root level="info"> |
| 34 | + <AppenderRef ref="JsonAppender"/> |
| 35 | + </Root> |
| 36 | + </Loggers> |
| 37 | +</Configuration> |
| 38 | +``` |
| 39 | + |
| 40 | +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. |
| 41 | + |
| 42 | +## Standard structured keys |
| 43 | + |
| 44 | +Your Logger will always include the following keys to your structured logging: |
| 45 | + |
| 46 | +Key | Type | Example | Description |
| 47 | +------------------------------------------------- | ------------------------------------------------- | --------------------------------------------------------------------------------- | ------------------------------------------------- |
| 48 | +**timestamp** | String | "2020-05-24 18:17:33,774" | Timestamp of actual log statement |
| 49 | +**level** | String | "INFO" | Logging level |
| 50 | +**coldStart** | Boolean | true| ColdStart value. |
| 51 | +**service** | String | "payment" | Service name defined. "service_undefined" will be used if unknown |
| 52 | +**message** | String | "Collecting payment" | Log statement value. Unserializable JSON values will be casted to string |
| 53 | +**functionName**| String | "example-powertools-HelloWorldFunction-1P1Z6B39FLU73" |
| 54 | +**functionVersion**| String | "12" |
| 55 | +**functionMemorySize**| String | "128" |
| 56 | +**functionArn**| String | "arn:aws:lambda:eu-west-1:012345678910:function:example-powertools-HelloWorldFunction-1P1Z6B39FLU73" |
| 57 | + |
| 58 | + |
| 59 | +## Capturing context Lambda info |
| 60 | + |
| 61 | +You can enrich your structured logs with key Lambda context information via `logEvent` annotation parameter. |
| 62 | + |
| 63 | +```java:title=App.java |
| 64 | +package helloworld; |
| 65 | + |
| 66 | +import org.apache.logging.log4j.LogManager; |
| 67 | +import org.apache.logging.log4j.Logger; |
| 68 | +import software.amazon.lambda.logging.PowerLogger; |
| 69 | +import software.amazon.lambda.logging.PowerToolsLogging; |
| 70 | +... |
| 71 | + |
| 72 | +/** |
| 73 | + * Handler for requests to Lambda function. |
| 74 | + */ |
| 75 | +public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
| 76 | + |
| 77 | + Logger log = LogManager.getLogger(); |
| 78 | + |
| 79 | + @PowerToolsLogging |
| 80 | + public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { |
| 81 | + ... |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +You can also explicitly log any incoming event using `logEvent` param. |
| 87 | + |
| 88 | +<Note type="warning"> |
| 89 | + This is disabled by default to prevent sensitive info being logged. |
| 90 | +</Note><br/> |
| 91 | + |
| 92 | +```java:title=App.java |
| 93 | +package helloworld; |
| 94 | + |
| 95 | +import org.apache.logging.log4j.LogManager; |
| 96 | +import org.apache.logging.log4j.Logger; |
| 97 | +import software.amazon.lambda.logging.PowerLogger; |
| 98 | +import software.amazon.lambda.logging.PowerToolsLogging; |
| 99 | +... |
| 100 | + |
| 101 | +/** |
| 102 | + * Handler for requests to Lambda function. |
| 103 | + */ |
| 104 | +public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
| 105 | + |
| 106 | + Logger log = LogManager.getLogger(); |
| 107 | + |
| 108 | + @PowerToolsLogging(logEvent = true) |
| 109 | + public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { |
| 110 | + ... |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Appending additional keys |
| 116 | + |
| 117 | +You can append your own keys to your existing Logger via `customKey`. |
| 118 | + |
| 119 | +```java:title=App.java |
| 120 | +package helloworld; |
| 121 | + |
| 122 | +import org.apache.logging.log4j.LogManager; |
| 123 | +import org.apache.logging.log4j.Logger; |
| 124 | +import software.amazon.lambda.logging.PowerLogger; |
| 125 | +import software.amazon.lambda.logging.PowerToolsLogging; |
| 126 | +... |
| 127 | + |
| 128 | +/** |
| 129 | + * Handler for requests to Lambda function. |
| 130 | + */ |
| 131 | +public class App implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
| 132 | + |
| 133 | + Logger log = LogManager.getLogger(); |
| 134 | + |
| 135 | + @PowerToolsLogging(logEvent = true) |
| 136 | + public APIGatewayProxyResponseEvent handleRequest(final APIGatewayProxyRequestEvent input, final Context context) { |
| 137 | + ... |
| 138 | + PowerLogger.customKey("test", "willBeLogged"); |
| 139 | + ... |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
0 commit comments