This class serves as a programmatic alternative to AspectJ-based {@code @Logging} annotation,
* allowing developers to integrate Powertools logging capabilities without AspectJ dependencies.
- *
+ *
* - Lambda context initialization with function metadata, trace ID, and service name
@@ -77,6 +77,10 @@ public final class PowertoolsLogging implements Resource {
Core.getGlobalContext().register(INSTANCE);
}
+ private PowertoolsLogging() {
+ // Utility class
+ }
+
public static void init() {
// Placeholder method used to enable SnapStart priming. Users need a direct reference to this class in order
// for the CRaC hooks to execute.
@@ -90,7 +94,8 @@ private static void initializeLogLevel() {
Level lambdaLevel = getLevelFromString(LAMBDA_LOG_LEVEL);
if (powertoolsLevel.toInt() < lambdaLevel.toInt()) {
LOG.warn(
- "Current log level ({}) does not match AWS Lambda Advanced Logging Controls minimum log level ({}). This can lead to data loss, consider adjusting them.",
+ "Current log level ({}) does not match AWS Lambda Advanced Logging Controls minimum log " +
+ "level ({}). This can lead to data loss, consider adjusting them.",
POWERTOOLS_LOG_LEVEL, LAMBDA_LOG_LEVEL);
}
}
@@ -146,10 +151,10 @@ public static void clearBuffer() {
* Initializes Lambda logging context with standard Powertools fields.
* This method should be called at the beginning of your Lambda handler to set up
* logging context with Lambda function information, trace ID, and service name.
- *
+ *
* Important: Call {@link #clearState(boolean)} at the end of your handler or use
* {@link #withLogging(Context, Supplier)} to handle cleanup automatically.
- *
+ *
* @param context the Lambda context provided by AWS Lambda runtime
*/
public static void initializeLogging(Context context) {
@@ -160,11 +165,11 @@ public static void initializeLogging(Context context) {
* Initializes Lambda logging context with sampling rate configuration.
* This method sets up logging context and optionally enables DEBUG logging
* based on the provided sampling rate.
- *
+ *
* Important: Call {@link #clearState(boolean)} at the end of your handler or use
* {@link #withLogging(Context, double, Supplier)} to handle cleanup automatically.
- *
- * @param context the Lambda context provided by AWS Lambda runtime
+ *
+ * @param context the Lambda context provided by AWS Lambda runtime
* @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
*/
public static void initializeLogging(Context context, double samplingRate) {
@@ -175,13 +180,13 @@ public static void initializeLogging(Context context, double samplingRate) {
* Initializes Lambda logging context with correlation ID extraction.
* This method sets up logging context and extracts correlation ID from the event
* using the provided JSON path.
- *
+ *
* Important: Call {@link #clearState(boolean)} at the end of your handler or use
* {@link #withLogging(Context, String, Object, Supplier)} to handle cleanup automatically.
- *
- * @param context the Lambda context provided by AWS Lambda runtime
+ *
+ * @param context the Lambda context provided by AWS Lambda runtime
* @param correlationIdPath JSON path to extract correlation ID from event
- * @param event the Lambda event object
+ * @param event the Lambda event object
*/
public static void initializeLogging(Context context, String correlationIdPath, Object event) {
initializeLogging(context, 0.0, correlationIdPath, event);
@@ -192,16 +197,16 @@ public static void initializeLogging(Context context, String correlationIdPath,
* This method sets up logging context with Lambda function information,
* configures sampling rate for DEBUG logging, and optionally extracts
* correlation ID from the event.
- *
+ *
* Important: Call {@link #clearState(boolean)} at the end of your handler or use
* {@link #withLogging(Context, double, String, Object, Supplier)} to handle cleanup automatically.
- *
+ *
* This method is thread-safe.
- *
- * @param context the Lambda context provided by AWS Lambda runtime
- * @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
+ *
+ * @param context the Lambda context provided by AWS Lambda runtime
+ * @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
* @param correlationIdPath JSON path to extract correlation ID from event (can be null)
- * @param event the Lambda event object (required if correlationIdPath is provided)
+ * @param event the Lambda event object (required if correlationIdPath is provided)
*/
public static void initializeLogging(Context context, double samplingRate, String correlationIdPath, Object event) {
@@ -260,7 +265,8 @@ private static double getEffectiveSamplingRate(double samplingRate) {
return Double.parseDouble(envSampleRate);
} catch (NumberFormatException e) {
LOG.warn(
- "Skipping sampling rate on environment variable configuration because of invalid value. Sampling rate: {}",
+ "Skipping sampling rate on environment variable configuration because of invalid value. " +
+ "Sampling rate: {}",
envSampleRate);
}
}
@@ -287,7 +293,7 @@ private static void captureCorrelationId(String correlationIdPath, Object event)
/**
* Clears MDC state and log buffer.
- *
+ *
* @param clearMdcState whether to clear MDC state
*/
public static void clearState(boolean clearMdcState) {
@@ -300,10 +306,10 @@ public static void clearState(boolean clearMdcState) {
/**
* Executes code with logging context initialized and automatically clears state.
- *
- * @param context the Lambda context provided by AWS Lambda runtime
+ *
+ * @param context the Lambda context provided by AWS Lambda runtime
* @param supplier the code to execute with logging context
- * @param the return type
+ * @param the return type
* @return the result of the supplier execution
*/
public static T withLogging(Context context, Supplier supplier) {
@@ -317,11 +323,11 @@ public static T withLogging(Context context, Supplier supplier) {
/**
* Executes code with logging context initialized with sampling rate and automatically clears state.
- *
- * @param context the Lambda context provided by AWS Lambda runtime
+ *
+ * @param context the Lambda context provided by AWS Lambda runtime
* @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
- * @param supplier the code to execute with logging context
- * @param the return type
+ * @param supplier the code to execute with logging context
+ * @param the return type
* @return the result of the supplier execution
*/
public static T withLogging(Context context, double samplingRate, Supplier supplier) {
@@ -335,12 +341,12 @@ public static T withLogging(Context context, double samplingRate, Supplier the return type
+ * @param event the Lambda event object
+ * @param supplier the code to execute with logging context
+ * @param the return type
* @return the result of the supplier execution
*/
public static T withLogging(Context context, String correlationIdPath, Object event, Supplier supplier) {
@@ -354,17 +360,17 @@ public static T withLogging(Context context, String correlationIdPath, Objec
/**
* Executes code with logging context initialized with full configuration and automatically clears state.
- *
- * @param context the Lambda context provided by AWS Lambda runtime
- * @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
+ *
+ * @param context the Lambda context provided by AWS Lambda runtime
+ * @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
* @param correlationIdPath JSON path to extract correlation ID from event (can be null)
- * @param event the Lambda event object (required if correlationIdPath is provided)
- * @param supplier the code to execute with logging context
- * @param the return type
+ * @param event the Lambda event object (required if correlationIdPath is provided)
+ * @param supplier the code to execute with logging context
+ * @param the return type
* @return the result of the supplier execution
*/
public static T withLogging(Context context, double samplingRate, String correlationIdPath, Object event,
- Supplier supplier) {
+ Supplier supplier) {
initializeLogging(context, samplingRate, correlationIdPath, event);
try {
return supplier.get();
From 0cef243da8dc1ace345e7c21355e8aa3dbbf379a Mon Sep 17 00:00:00 2001
From: David-DAM <82216301+David-DAM@users.noreply.github.com>
Date: Thu, 8 Oct 2026 16:17:36 +0200
Subject: [PATCH 5/5] Update documentation for Lambda SnapStart priming with
`PowertoolsLogging.init` usage examples
---
docs/core/logging.md | 128 +++++++++++++++++++++----------------------
1 file changed, 64 insertions(+), 64 deletions(-)
diff --git a/docs/core/logging.md b/docs/core/logging.md
index 4a7b2b13e..88cbb1be2 100644
--- a/docs/core/logging.md
+++ b/docs/core/logging.md
@@ -1339,6 +1339,70 @@ sequenceDiagram
8. **What happens if Lambda times out without flushing the buffer?** Logs that are still in the buffer will be lost.
9. **How does the `BufferingAppender` work with different appenders?** The `BufferingAppender` is designed to wrap arbitrary appenders, providing maximum flexibility. You can wrap console appenders, file appenders, or any custom appenders with buffering functionality.
+### Lambda SnapStart priming
+
+The PowertoolsLogging class integrates with AWS Lambda SnapStart to improve restore durations. To make sure the SnapStart
+priming logic of this class runs correctly, you need an explicit reference to `PowertoolsLogging` in your code to allow the
+library to register before SnapStart takes a memory snapshot. Learn more about what priming is in
+this [blog post](https://aws.amazon.com/blogs/compute/optimizing-cold-start-performance-of-aws-lambda-using-advanced-priming-strategies-with-snapstart/)
+{target="_blank"}.
+
+Make sure to call `PowertoolsLogging.init()` in your Lambda handler initialization code. This can be done by adding one of the following lines to your handler class:
+
+=== "Constructor"
+
+ ```java hl_lines="12"
+ import org.slf4j.Logger;
+ import org.slf4j.LoggerFactory;
+ import software.amazon.lambda.powertools.logging.Logging;
+ import software.amazon.lambda.powertools.logging.PowertoolsLogging;
+ // ... other imports
+
+ public class MyFunctionHandler implements RequestHandler {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(MyFunctionHandler.class);
+
+ public MyFunctionHandler() {
+ PowertoolsLogging.init(); // Ensure PowertoolsLogging is loaded for SnapStart
+ }
+
+ @Override
+ @Logging
+ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
+ LOGGER.info("Example logging");
+ // ...
+ return something;
+ }
+ }
+ ```
+
+=== "Static Initializer"
+
+ ```java hl_lines="12"
+ import org.slf4j.Logger;
+ import org.slf4j.LoggerFactory;
+ import software.amazon.lambda.powertools.logging.Logging;
+ import software.amazon.lambda.powertools.logging.PowertoolsLogging;
+ // ... other imports
+
+ public class MyFunctionHandler implements RequestHandler {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(MyFunctionHandler.class);
+
+ static {
+ PowertoolsLogging.init(); // Ensure PowertoolsLogging is loaded for SnapStart
+ }
+
+ @Override
+ @Logging
+ public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
+ LOGGER.info("Example logging");
+ // ...
+ return something;
+ }
+ }
+ ```
+
## Sampling debug logs
You can dynamically set a percentage of your logs to`DEBUG` level to be included in the logger output, regardless of configured log level, using the`POWERTOOLS_LOGGER_SAMPLE_RATE` environment variable,
@@ -1645,67 +1709,3 @@ Use the `LambdaEcsEncoder` rather than the `LambdaJsonEncoder` when configuring
```
-
-## Advanced
-
-### Lambda SnapStart priming
-
-The PowertoolsLogging class integrates with AWS Lambda SnapStart to improve restore durations. To make sure the SnapStart
-priming logic of this class runs correctly, you need an explicit reference to `PowertoolsLogging` in your code to allow the
-library to register before SnapStart takes a memory snapshot. Learn more about what priming is in
-this [blog post](https://aws.amazon.com/blogs/compute/optimizing-cold-start-performance-of-aws-lambda-using-advanced-priming-strategies-with-snapstart/)
-{target="_blank"}.
-
-Make sure to call `PowertoolsLogging.init()` in your Lambda handler initialization code. This can be done by adding one of the following lines to your handler class:
-
-=== "Constructor"
-
- ```java hl_lines="11"
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import software.amazon.lambda.powertools.logging.Logging;
- // ... other imports
-
- public class MyFunctionHandler implements RequestHandler {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(MyFunctionHandler.class);
-
- public MyFunctionHandler() {
- PowertoolsLogging.init(); // Ensure PowertoolsLogging is loaded for SnapStart
- }
-
- @Override
- @Logging
- public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
- LOGGER.info("Example logging");
- // ...
- return something;
- }
- }
- ```
-
-=== "Static Initializer"
-
- ```java hl_lines="11"
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import software.amazon.lambda.powertools.logging.Logging;
- // ... other imports
-
- public class MyFunctionHandler implements RequestHandler {
-
- private static final Logger LOGGER = LoggerFactory.getLogger(MyFunctionHandler.class);
-
- static {
- PowertoolsLogging.init(); // Ensure PowertoolsLogging is loaded for SnapStart
- }
-
- @Override
- @Logging
- public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
- LOGGER.info("Example logging");
- // ...
- return something;
- }
- }
- ```