CDI logging-interceptor Download Join the chat at https://gitter.im/t1/logging-interceptor Build Status Java CI

CDI interceptor for logging to slf4j.

Logging is the standard example for writing interceptors. Yet, I didn't find a good one, so I wrote my own (and it was a lot of fun :)

There are two main use-cases for logging interceptors:

News

Version 3.x

We use semantic versioning. tl;dr: versions consist of three parts with a semantic: The Bad (major = breaking changes), the Good (minor = new features), and the Ugly (micro/patch = bugfixes).

So going to 3.0.0 was Bad, as it may break existing applications. But sometimes Bad things are necessary. Here we need it to get to Java 8 and replace esp. Joda-Date (BTW: big kudos to Stephen for that project and for making it obsolete by moving it to Java 8).

Features

Examples

Basic Trace

@Path("/customers")
@Logged(level = INFO)
public class CustomersResource {
    @GET
    @Path("/{customer-id}")
    public Customer getCustomer(@PathParam("customer-id") String customerId) {
        return ...
    }
}

would log calls to all methods in CustomersResource at INFO level, e.g.:

get customer 1234
return Customer(id=1234, firstName="Joe", ...)

Classic Logger

static class CustomersResourceLogger {
    @Logged("found {} for id {}")
    void foundCustomerForId(Customer customer, String customerId) {}
}

@Inject CustomersResourceLogger log;

...
log.foundCustomerForId(customer, "1234");
...

would log:

found Customer(id=1234, firstName="Joe", ...) for id 1234

Log Stack Trace

static class ExceptionLogger {
    @Logged(level = ERROR)
    void failed(String operation, RuntimeException e) {}
}

@Inject
ExceptionLogger exceptionLogger;

...
try {
    ...
} catch (RuntimeException e) {
    exceptionLogger.failed("my operation", e);
}
...

would log the message failed my operation with the exception and stack trace.

Converter

public class ResponseLogConverter implements Converter {
    public String convert(Response response) {
        StatusType statusInfo = response.getStatusInfo();
        return statusInfo.getStatusCode() + " " + statusInfo.getReasonPhrase() + entityInfo(response);
    }

    private String entityInfo(Response response) {
        Object entity = response.getEntity();
        if (entity == null)
            return "";
        return ": " + entity.getClass().getSimpleName();
    }
}

Download

Add Bintray to your settings.xml (see the Set me up! button) and this Maven dependency to your pom.xml:

<dependency>
  <groupId>com.github.t1</groupId>
  <artifactId>logging-interceptor</artifactId>
  <version>${logging-interceptor.version}</version>
</dependency>

Enable in Java EE 7

The interceptor is annotated with a @Priority (see the last paragraph in Java EE Tutorial). So it is automatically activated if you add it as a library to you application.

Enable in Java EE 6

Enabling interceptors from a library jar is a bit tricky in CDI 1.0. If you'd just add the logging-interceptor.jar to your war or ear, it was a separate CDI module, and the interceptor was not useable in your application. So you'll have to overlay the jar contents into your application by adding this to your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <overlays>
                    <overlay>
                        <groupId>com.github.t1</groupId>
                        <artifactId>logging-interceptor</artifactId>
                        <type>jar</type>
                        <targetPath>WEB-INF/classes</targetPath>
                    </overlay>
                </overlays>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.github.t1</groupId>
        <artifactId>logging-interceptor</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

Then you can activate the interceptor in the application's beans.xml:

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
    <interceptors>
        <class>com.github.t1.log.LoggingInterceptor</class>
    </interceptors>
</beans>

License

Licensed under Apache License 2.0