Sanctions via Boycotting

Following the recent event regarding Huawei CFO being held in Canada, and then two Canadians being arrested in China. It has been all over the news for the past few days. The public sentiment and…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Create a Spring Boot initializer that intercepts the logs and sends them to FluentD

Before we get started, it’s important to make sure you have the following prerequisites installed on your machine:

With that out of the way, let’s dive in!

There are a few reasons why you might want to use Fluentd in your application:

Before we can send logs from our Spring application to Fluentd, we need to set up a Fluentd server and configure it to receive logs. Here’s a quick overview of the steps involved:

Here’s a sample configuration file that listens for HTTP POST requests on port 24224 and sends the logs to a file named access.log:

Now that we have a Fluentd server set up and running, let’s create a Spring Initializer project to send logs to it. If you’re not familiar with Spring Initializer, it’s a quick way to create a new Spring Boot Project.

That’s it! You now have a basic Spring Boot project set up and ready to go.

Next, we’ll implement an interceptor to capture logs in our Spring application. An interceptor is a component that intercepts incoming requests and outgoing responses to perform certain actions. In this case, we’ll use an interceptor to intercept the log messages generated by our application and send them to Fluentd.

To create an interceptor, we’ll need to create a class that implements the HandlerInterceptor interface. This interface has three methods that we can override:

Here’s an example of an interceptor that captures the log messages generated by our application and sends them to Fluentd:

In the `preHandle()` method, we’re adding a request ID to the MDC. This request ID will be included in all the log messages generated by the application during the handling of the request.

In the `postHandle()` method, we’re sending the captured log messages to Fluentd using the `fluentdClient.send()` method. The `send()` method takes two arguments: a tag and a data map. The tag is used to classify the log message and can be used to route the log message to different destinations. The data map is a key-value map of the log data.

Finally, in the `afterCompletion()` method, we’re clearing the MDC data to prevent any memory leaks.

To send the intercepted logs to Fluentd, we’ll need to use the Fluentd Java library. First, add the following dependency to your pom.xml file:

Next, create a FluentdClient class to wrap the Fluentd Java library:

This FluentdClient class uses the FluentLogger class from the Fluentd Java library to send log messages to Fluentd. The getLogger() method takes three arguments: the tag prefix, the hostname of the Fluentd server, and the port number of the Fluentd server. The log() method sends a log message to Fluentd with the specified tag and data.

Now that we have the FluentdClient set up, we can use it in our LogInterceptor to send the intercepted log messages to Fluentd. Simply inject an instance of FluentdClient into the LogInterceptor and use it in the postHandle() method to send the log messages to Fluentd:

That’s it! You now have a Spring Initializer project that captures the logs generated by your application and sends them to Fluentd using the Fluentd Java library.

Add a comment

Related posts:

Open Your Presence Before Christmas!

This poem came to me one morning. I kept hearing, “Open Your Presence Before Christmas.”. “Open Your Presence Before Christmas!” is published by Dr Jaena Stanley-Gonzaga.

Forever loved

Christian faith is not about a religion or a set of right doctrinal statement or of any particular movement or emphasis. Most foundationally a relationship with God in love, because of the abounding…

What if Cambridge Analytica Sued Facebook?

Now that Cambridge Analytica has been driven into bankruptcy by the Facebook data breach scandal, trade libel law would provide an interesting vehicle for the hypothetical Cambridge Analytica v…