Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Conversation

phipag
Copy link
Contributor

@phipag phipag commented Oct 17, 2025

Summary

This PR introduces an alternative functional interface to the Logging utility that does not depend on the AspectJ @Logging annotation. We re-use PowertoolsLogging as entry point for the Powertools logging experience which exposes two new methods:

  1. initializeLogging() – has some method overloads for convenience
  2. clearState() – can optionally clear the logging context as well

This is also thread-safe now.

We also add a new E2E test asserting that it yields the exact same outputs as the AspectJ aspect based way.

The before and after compared to AspectJ looks like this:

Usage with AspectJ

public class App implements RequestHandler<APIGatewayProxyRequestEvent, String> {
    private static final Logger log = LoggerFactory.getLogger(App.class);

    @Logging(logEvent = true, logResponse = true, samplingRate = 0.7, correlationIdPath = "requestContext.requestId")
    public String handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        log.info("Processing request");
        return "Hello World";
    }
}

Usage without AspectJ

public class App implements RequestHandler<APIGatewayProxyRequestEvent, String> {
    private static final Logger log = LoggerFactory.getLogger(App.class);

    public String handleRequest(APIGatewayProxyRequestEvent input, Context context) {
        try {
                PowertoolsLogging.initializeLogging(context, 0.7, "requestContext.requestId", input);
                
                // Manual event logging (if desired)
                log.info("Handler Event", entry("event", input));
                
                log.info("Processing request");
                String response = "Hello World";
                
                // Manual response logging (if desired)
                log.info("Handler Response", entry("response", response));
                
                return response;
        } finally {
            PowertoolsLogging.clearState()
        }
    }
}

Note: In this small RFC I am investigating an improvement to this imperative API leveraging a more modern functional Middleware approach similar to TypeScript middy.js. See #2202

Changes

Issue number: #2204


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@phipag phipag marked this pull request as ready for review October 20, 2025 07:43
@hjgraca
Copy link
Collaborator

hjgraca commented Oct 20, 2025

Just one small observation on the API design and developer experience.
Unfortunately we need to have try... finally block, but will developers consistently remember to call clearState() in finally blocks?

Could we add to the API the possibility to create a functional wrapper that handles the try-finally automatically? I think this is what you mention in the #2202 issue

For example:

PowertoolsLogging.withLogging(context, config, () -> {
    log.info("Processing request");
    return "Hello World";
});

powertools-logging/pom.xml Show resolved Hide resolved
@phipag
Copy link
Contributor Author

phipag commented Oct 20, 2025

Just one small observation on the API design and developer experience. Unfortunately we need to have try... finally block, but will developers consistently remember to call clearState() in finally blocks?

Could we add to the API the possibility to create a functional wrapper that handles the try-finally automatically? I think this is what you mention in the #2202 issue

For example:

PowertoolsLogging.withLogging(context, config, () -> {
    log.info("Processing request");
    return "Hello World";
});

Yes, I like this idea. I'll implement such a wrapper function.

Correct, issue #2202 will abstract such wrappers away from individual utilities to a middleware approach which is more scalable. But I like the idea of having a functional wrapper for an individual utility as well for customers who simply want to get started with a utility.

@phipag phipag changed the title feat(logging): Support functional interface in addition to AspectJ @Logging annotation feat(logging): Support functional interface in addition to AspectJ @Logging annotation Oct 21, 2025
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Feature request: Support functional interface for Logging utility

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.