Log incoming HTTP Request headers with a Server Filter
Learn to log every HTTP Request header with a @ServerFilter and a method annotated with @FilterRequest.
On this guide
In this section
Getting Started
In this guide, we will create a Micronaut application written in Java.
What you will need
To complete this guide, you will need the following:
-
Some time on your hands
-
A decent text editor or IDE (e.g. IntelliJ IDEA)
-
JDK 21 or greater installed with
JAVA_HOMEconfigured appropriately
Solution
We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.
-
Download and unzip the source
Writing the Application
Create an application using the Micronaut Command Line Interface or with Micronaut Launch.
mn create-app example.micronaut.micronautguide \
--features= \
--build=gradle \
--lang=java \
--test=junit|
Note
|
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool. If you don’t specify the --lang argument, Java is used as the language.If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.
|
The previous command creates a Micronaut application with the default package example.micronaut in a directory named micronautguide.
If you use Micronaut Launch, select Micronaut Application as application type and add features.
|
Note
|
If you have an existing Micronaut application and want to add the functionality described here, you can view the dependency and configuration changes from the specified features, and apply those changes to your application. |
Logback
Configure Logback to log the package example.micronaut with TRACE level.
<configuration>
...
<logger name="example.micronaut" level="TRACE"/>
</configuration>ServerFilter
Create a filter which logs the non-sensitive HTTP request headers.
Controller
Create a controller that responds with a JSON object: {"message":"Hello World"}.
Running the Application
To run the application, use the ./gradlew run command, which starts the application on port 8080.
curl localhost:8080 -H "X-Request-Id: 1234"You will see in the logs:
... TRACE e.micronaut.LoggingHeadersFilter - Host: localhost:8080
... TRACE e.micronaut.LoggingHeadersFilter - User-Agent: curl/8.4.0
... TRACE e.micronaut.LoggingHeadersFilter - Accept: */*
... TRACE e.micronaut.LoggingHeadersFilter - X-Request-Id: 1234Tests
Logback Dependency in test scope
In addition to the runtime classpath, add the logback-classic dependency to the test classpath:
testImplementation("ch.qos.logback:logback-classic")Write tests
Create a MemoryAppender to ease testing.
package example.micronaut;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;
import java.util.ArrayList;
import java.util.List;
public class MemoryAppender extends AppenderBase<ILoggingEvent> {
private final List<ILoggingEvent> events = new ArrayList<>();
@Override
protected void append(ILoggingEvent e) {
events.add(e);
}
public List<ILoggingEvent> getEvents() {
return events;
}
}Create a test that verifies the filter logs HTTP headers and masks sensitive HTTP headers.
Testing the Application
To run the tests:
./gradlew testThen open build/reports/tests/test/index.html in a browser to see the results.
Next Steps
Explore more features with Micronaut Guides.
Learn more about Filter Methods.
Help with the Micronaut Framework
The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.
License
|
Note
|
All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images). |