Logging
Logging 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, can be enabled explicitly via annotation param
- Append additional keys to structured log at any point in time
Initialization¶
Powertools for AWS Lambda (Java) extends the functionality of Log4J. Below is an example log4j2.xml
file, with the JsonTemplateLayout
using LambdaJsonLayout.json
configured.
LambdaJsonLayout is now deprecated
Configuring utiltiy using <LambdaJsonLayout/>
plugin is deprecated now. While utility still supports the old configuration, we strongly recommend upgrading the
log4j2.xml
configuration to JsonTemplateLayout
instead. JsonTemplateLayout is recommended way of doing structured logging.
Please follow this guide for upgrade steps.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
You can also override log level by setting POWERTOOLS_LOG_LEVEL
env var. Here is an example using AWS Serverless Application Model (SAM)
1 2 3 4 5 6 7 8 9 10 |
|
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 logs 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 |
samplingRate | int | 0.1 | Debug logging sampling rate in percentage e.g. 10% in this case |
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" | |
xray_trace_id | String | "1-5759e988-bd862e3fe1be46a994272793" | X-Ray Trace ID when Lambda function has enabled Tracing |
function_request_id | String | "899856cb-83d1-40d7-8611-9e78f15f32f4"" | AWS Request ID from lambda context |
Capturing context Lambda info¶
You can enrich your structured logs with key Lambda context information via logEvent
annotation parameter.
You can also explicitly log any incoming event using logEvent
param. Refer Override default object mapper
to customise what is logged.
Warning
Log event is disabled by default to prevent sensitive info being logged.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Customising fields in logs¶
- Utility by default emits
timestamp
field in the logs in formatyyyy-MM-dd'T'HH:mm:ss.SSSZz
and in system default timezone. If you need to customize format and timezone, you can do so by configuringlog4j2.component.properties
and configuring properties as shown in example below:
1 2 |
|
-
Utility also provides sample template for Elastic Common Schema(ECS) layout. The field emitted in logs will follow specs from ECS together with field captured by utility as mentioned above.
Use
LambdaEcsLayout.json
aseventTemplateUri
when configuringJsonTemplateLayout
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Setting a Correlation ID¶
You can set a Correlation ID using correlationIdPath
attribute by passing a JSON Pointer expression.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
We provide built-in JSON Pointer expression for known event sources, where either a request ID or X-Ray Trace ID are present.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Appending additional keys¶
Custom keys are persisted across warm invocations
Always set additional keys as part of your handler to ensure they have the latest value, or explicitly clear them with clearState=true
.
You can append your own keys to your existing logs via appendKey
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Removing additional keys¶
You can remove any additional key from entry using LoggingUtils.removeKeys()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Clearing all state¶
Logger is commonly initialized in the global scope. Due to Lambda Execution Context reuse,
this means that custom keys can be persisted across invocations. If you want all custom keys to be deleted, you can use
clearState=true
attribute on @Logging
annotation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 10 11 |
|
Override default object mapper¶
You can optionally choose to override default object mapper which is used to serialize lambda function events. You might want to supply custom object mapper in order to control how serialisation is done, for example, when you want to log only specific fields from received event due to security.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Sampling debug logs¶
You can dynamically set a percentage of your logs to DEBUG level via env var POWERTOOLS_LOGGER_SAMPLE_RATE
or
via samplingRate
attribute on annotation.
Info
Configuration on environment variable is given precedence over sampling rate configuration on annotation, provided it's in valid value range.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
1 2 3 4 5 6 7 8 9 |
|
Upgrade to JsonTemplateLayout from deprecated LambdaJsonLayout configuration in log4j2.xml¶
Prior to version 1.10.0, only supported way of configuring log4j2.xml
was via <LambdaJsonLayout/>
. This plugin is
deprecated now and will be removed in future version. Switching to JsonTemplateLayout
is straight forward.
Below examples shows deprecated and new configuration of log4j2.xml
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|