3030import java .util .Locale ;
3131import java .util .Random ;
3232import java .util .concurrent .atomic .AtomicBoolean ;
33+ import java .util .function .Supplier ;
3334
3435import org .slf4j .Logger ;
3536import org .slf4j .LoggerFactory ;
@@ -140,6 +141,9 @@ public static void clearBuffer() {
140141 * This method should be called at the beginning of your Lambda handler to set up
141142 * logging context with Lambda function information, trace ID, and service name.
142143 *
144+ * <p>Important: Call {@link #clearState(boolean)} at the end of your handler or use
145+ * {@link #withLogging(Context, Supplier)} to handle cleanup automatically.</p>
146+ *
143147 * @param context the Lambda context provided by AWS Lambda runtime
144148 */
145149 public static void initializeLogging (Context context ) {
@@ -151,6 +155,9 @@ public static void initializeLogging(Context context) {
151155 * This method sets up logging context and optionally enables DEBUG logging
152156 * based on the provided sampling rate.
153157 *
158+ * <p>Important: Call {@link #clearState(boolean)} at the end of your handler or use
159+ * {@link #withLogging(Context, double, Supplier)} to handle cleanup automatically.</p>
160+ *
154161 * @param context the Lambda context provided by AWS Lambda runtime
155162 * @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
156163 */
@@ -163,6 +170,9 @@ public static void initializeLogging(Context context, double samplingRate) {
163170 * This method sets up logging context and extracts correlation ID from the event
164171 * using the provided JSON path.
165172 *
173+ * <p>Important: Call {@link #clearState(boolean)} at the end of your handler or use
174+ * {@link #withLogging(Context, String, Object, Supplier)} to handle cleanup automatically.</p>
175+ *
166176 * @param context the Lambda context provided by AWS Lambda runtime
167177 * @param correlationIdPath JSON path to extract correlation ID from event
168178 * @param event the Lambda event object
@@ -177,7 +187,10 @@ public static void initializeLogging(Context context, String correlationIdPath,
177187 * configures sampling rate for DEBUG logging, and optionally extracts
178188 * correlation ID from the event.
179189 *
180- * This method is tread-safe.
190+ * <p>Important: Call {@link #clearState(boolean)} at the end of your handler or use
191+ * {@link #withLogging(Context, double, String, Object, Supplier)} to handle cleanup automatically.</p>
192+ *
193+ * <p>This method is thread-safe.</p>
181194 *
182195 * @param context the Lambda context provided by AWS Lambda runtime
183196 * @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
@@ -278,4 +291,79 @@ public static void clearState(boolean clearMdcState) {
278291 clearBuffer ();
279292 SAMPLER .remove ();
280293 }
294+
295+ /**
296+ * Executes code with logging context initialized and automatically clears state.
297+ *
298+ * @param context the Lambda context provided by AWS Lambda runtime
299+ * @param supplier the code to execute with logging context
300+ * @param <T> the return type
301+ * @return the result of the supplier execution
302+ */
303+ public static <T > T withLogging (Context context , Supplier <T > supplier ) {
304+ initializeLogging (context );
305+ try {
306+ return supplier .get ();
307+ } finally {
308+ clearState (true );
309+ }
310+ }
311+
312+ /**
313+ * Executes code with logging context initialized with sampling rate and automatically clears state.
314+ *
315+ * @param context the Lambda context provided by AWS Lambda runtime
316+ * @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
317+ * @param supplier the code to execute with logging context
318+ * @param <T> the return type
319+ * @return the result of the supplier execution
320+ */
321+ public static <T > T withLogging (Context context , double samplingRate , Supplier <T > supplier ) {
322+ initializeLogging (context , samplingRate );
323+ try {
324+ return supplier .get ();
325+ } finally {
326+ clearState (true );
327+ }
328+ }
329+
330+ /**
331+ * Executes code with logging context initialized with correlation ID extraction and automatically clears state.
332+ *
333+ * @param context the Lambda context provided by AWS Lambda runtime
334+ * @param correlationIdPath JSON path to extract correlation ID from event
335+ * @param event the Lambda event object
336+ * @param supplier the code to execute with logging context
337+ * @param <T> the return type
338+ * @return the result of the supplier execution
339+ */
340+ public static <T > T withLogging (Context context , String correlationIdPath , Object event , Supplier <T > supplier ) {
341+ initializeLogging (context , correlationIdPath , event );
342+ try {
343+ return supplier .get ();
344+ } finally {
345+ clearState (true );
346+ }
347+ }
348+
349+ /**
350+ * Executes code with logging context initialized with full configuration and automatically clears state.
351+ *
352+ * @param context the Lambda context provided by AWS Lambda runtime
353+ * @param samplingRate sampling rate for DEBUG logging (0.0 to 1.0)
354+ * @param correlationIdPath JSON path to extract correlation ID from event (can be null)
355+ * @param event the Lambda event object (required if correlationIdPath is provided)
356+ * @param supplier the code to execute with logging context
357+ * @param <T> the return type
358+ * @return the result of the supplier execution
359+ */
360+ public static <T > T withLogging (Context context , double samplingRate , String correlationIdPath , Object event ,
361+ Supplier <T > supplier ) {
362+ initializeLogging (context , samplingRate , correlationIdPath , event );
363+ try {
364+ return supplier .get ();
365+ } finally {
366+ clearState (true );
367+ }
368+ }
281369}
0 commit comments