feat(logging): Support functional interface in addition to AspectJ @Logging annotation#2205
feat(logging): Support functional interface in addition to AspectJ @Logging annotation#2205
@Logging annotation#2205Conversation
…on is not guaranteed.
…e class is in internal package.
|
Just one small observation on the API design and developer experience. 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. |
@Logging annotation
|
|
Thanks @hjgraca. I implemented your suggestions. See this file for how it is used in E2E tests: https://github.com/aws-powertools/powertools-lambda-java/pull/2205/files#diff-3bcb5e87cb7cc4655b7d9427e50b10a9546c2a92910f6ae860635a041db2ebc9 |



Summary
This PR introduces an alternative functional interface to the Logging utility that does not depend on the AspectJ
@Loggingannotation. We re-usePowertoolsLoggingas entry point for the Powertools logging experience which exposes two new methods:initializeLogging()– has some method overloads for convenienceclearState()– can optionally clear the logging context as wellThis 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
Usage without AspectJ
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.