Skip to content

Commit

Permalink
Add new logging features through annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chavjoh committed Feb 3, 2024
1 parent 4cc3092 commit e3d786b
Show file tree
Hide file tree
Showing 12 changed files with 445 additions and 104 deletions.
25 changes: 20 additions & 5 deletions README.md
Expand Up @@ -22,7 +22,7 @@ The dependency is available in maven central (see badge for version):
The logging of requests and responses is done through a filter that can be activated on a resource with:

```java
@Logged(requestBody = true, responseBody = true)
@Logged
```

It will add the following information to MDC for the request processing:
Expand All @@ -43,7 +43,22 @@ with the following MDC fields set:

* Response HTTP status
* Response duration in milliseconds
* Request and response body (if activated in annotation)

Additional logging features can be activated using properties of the annotation:

```java
@Logged(requestBody = {LOG, MDC}, responseBody = {LOG, MDC}, filersBody = {YourBodyFilter.class})
```

* **requestBody**
* `LOG`: Logging the request body in a new log line `Received [method] [URI] [body]`
* `MDC`: Logging the request body as MDC only in the `Processed ...` log line
* **responseBody**
* `LOG`: Logging the response body at the end of the `Processed ...` log line
* `MDC`: Logging the response body as MDC only in the `Processed ...` log line
* **filtersBody**: Classes implementing the functional interface
[LoggedBodyFilter](src/main/java/com/chavaillaz/jakarta/rs/LoggedBodyFilter.java) to filter any body
before writing it in logs, for example to remove sensitive data that could be present.

## Example

Expand All @@ -54,7 +69,7 @@ Given an endpoint on which users can create new articles, annotated with `@Logge
@Path("/article")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Logged(requestBody = true, responseBody = true)
@Logged(requestBody = MDC, responseBody = MDC)
public class ArticleResource {

@POST
Expand Down Expand Up @@ -96,8 +111,8 @@ with the following MDC fields:
## Extension

An example of extension of the filter is available
with [UserLogged](src%2Ftest%2Fjava%2Fcom%2Fchavaillaz%2Fjakarta%2Frs%2FUserLogged.java)
and [UserLoggedFilter](src%2Ftest%2Fjava%2Fcom%2Fchavaillaz%2Fjakarta%2Frs%2FUserLoggedFilter.java).
with [UserLogged](src/test/java/com/chavaillaz/jakarta/rs/UserLogged.java)
and [UserLoggedFilter](src/test/java/com/chavaillaz/jakarta/rs/UserLoggedFilter.java).
In this example, you can find the following customization of the original filter:

* Log new **user-id** field in MDC
Expand Down
38 changes: 34 additions & 4 deletions pom.xml
Expand Up @@ -6,7 +6,7 @@

<groupId>com.chavaillaz</groupId>
<artifactId>jaxrs-logging</artifactId>
<version>1.0-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>${project.groupId}.${project.artifactId}</name>
Expand Down Expand Up @@ -49,6 +49,13 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -84,14 +91,19 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.9.0</version>
<version>5.10.0</version>
<scope>test</scope>
</dependency>

Expand All @@ -115,8 +127,26 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<release>11</release>
<release>17</release>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.16.2</version>
<configuration>
<rulesUri>file:///${project.basedir}/versions.xml</rulesUri>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>display-dependency-updates</goal>
<goal>display-plugin-updates</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
Expand Down
36 changes: 30 additions & 6 deletions src/main/java/com/chavaillaz/jakarta/rs/Logged.java
Expand Up @@ -21,17 +21,41 @@
public @interface Logged {

/**
* Indicates if the request body must be as MDC field when processing and logging the request.
* Indicates how the request body must be logged.
*
* @return {@code true} to log the request body, {@code false} otherwise
* @return The types of logging to be done
*/
boolean requestBody() default false;
LogType[] requestBody() default {};

/**
* Indicates if the response body must be as MDC field when processing and logging the request.
* Indicates how the response body must be logged.
*
* @return {@code true} to log the response body, {@code false} otherwise
* @return The types of logging to be done
*/
boolean responseBody() default false;
LogType[] responseBody() default {};

/**
* Indicates which filters must be applied before logging the request or response body.
*
* @return The list of filters to be applied
*/
Class<? extends LoggedBodyFilter>[] filersBody() default {};

/**
* Type of logging to be applied to the request and response body.
*/
enum LogType {

/**
* Writes the element as a new log line.
*/
LOG,

/**
* Writes the element as MDC field of the processed log line from {@link LoggedFilter}.
*/
MDC

}

}
17 changes: 17 additions & 0 deletions src/main/java/com/chavaillaz/jakarta/rs/LoggedBodyFilter.java
@@ -0,0 +1,17 @@
package com.chavaillaz.jakarta.rs;

/**
* Functional interface to filter the content of a request and response body.
*/
@FunctionalInterface
public interface LoggedBodyFilter {

/**
* Filters the given body to update or delete possible sensitive elements.
*
* @param body The body content
* @return The filtered body content
*/
String filterBody(String body);

}

0 comments on commit e3d786b

Please sign in to comment.