@StaticLoggingProxy

Generate a logging wrapper for an interface at compile time.

@StaticLoggingProxy generates a wrapper class that logs every method call through the project’s HasLogger infrastructure and delegates to the real implementation.

Usage

Annotate the interface:

import com.svenruppert.proxybuilder.proxy.generated.annotations.StaticLoggingProxy;

@StaticLoggingProxy
public interface Service {
  String work(String input);
}

After mvn compile, the processor generates ServiceStaticLoggingProxy in target/generated-sources/annotations/. Use it like this:

Service proxy = new ServiceStaticLoggingProxy()
    .withDelegator(new ServiceImpl());

proxy.work("hello");   // logs the call, then runs ServiceImpl.work("hello")

What it logs

For each method on the interface:

  • Method name and argument values before the call.
  • Return value (or thrown exception) after the call.

Logging goes through the same HasLogger mixin that the rest of ProxyBuilder uses, so it routes to the logger your application has already configured.

Generated class shape

The generated class has:

  • A no-arg constructor.
  • A withDelegator(Service delegator) method that returns this for chaining.
  • One method per interface method, each logging and then delegating.

If you need to customise the suffix (StaticLoggingProxy is long), see the processor options for the -Aproxybuilder.suffix= flag.

When not to use it

  • The interface has methods you do not want logged (for example, methods that handle secrets). Either move them to a separate interface or write a custom processor that filters by annotation.
  • You want logging only in development. The generated class is in your jar, so the logging code is always present. To toggle, configure the logger level for the class.

Compile-time equivalent of pre/post actions

The runtime addIPreAction/addIPostAction hooks are configurable per proxy instance. @StaticLoggingProxy is the compile-time equivalent for the specific case of logging. For arbitrary pre/post hooks at compile time, write a custom processor.

Edit this page on GitHub ↗