Creating a Logging Application in RabbitMQ With Spring Boot and Logback
In this post, we take a look at how to log an application's logs in a RabbitMQ server to achieve centralized logging.
Join the DZone community and get the full member experience.
Join For FreeSince the introduction of microservices architectures, the way applications are architected, developed, and monitored has changed a lot. The biggest issue with microservicse architectures is the need for centralized logging and tracing.
All the logging libraries come up with diffrent appenders for pushing/publishing a message. For example:
Console Appender -> for console logging.
File Appender -> for logging in a file.
Etc.
In this post, I am going to share how to log an application's logs in a RabbitMQ server to achieve centralized logging. Using the RabbitMQ server, log messages can be fed to other downstream systems like an ELK stack-based app (Elasticsearch, Logstach, Kibana) or Splunk as well.
Logstash - can pull messages from any source through its configured plugins for source and destination.
Elasticsearch - Elasticsearch is a full-text search framework and has the ability to provide faster search using its indexing, which can get logs from Logstach.
Kibana - UI application for searching and analyzing log data, etc.
In following steps, I am going to show you how to push application logs to a RabbitMQ server in Spring Boot using logback. Let's start.
1. The RabbitMQ server must be installed and running on your local machine at this URL, http://localhost:15672.
RabbitMQ comes up with a default login credentials:
username: guest
password: guest
2. Create an exchange from the RabbitMQ console: exchangeName: my-exchange
.
3. Create a queue from the RabbitMQ console: queueName: log-message-queue
.
4. Bring the exchangename from the queue into the console from the exchange tab.
5. Create a Spring Boot project from spring.io, having these dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
6. Create a logback.xml file in the src\main\resource
folder with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="AMQP" class="org.springframework.amqp.rabbit.logback.AmqpAppender">
<layout>
<pattern><![CDATA[ %d %p %t [%c] - <%m>%n ]]></pattern>
</layout>
<exchangeName>my-exchange</exchangeName>
<host>localhost</host>
<port>5672</port>
<username>guest</username>
<password>guest</password>
<exchangeType>queue</exchangeType>
<applicationId>AmqpAppenderTest</applicationId>
<routingKeyPattern>logs-test</routingKeyPattern>
<generateId>true</generateId>
<charset>UTF-8</charset>
<durable>false</durable>
<deliveryMode>NON_PERSISTENT</deliveryMode>
</appender>
<root level="info">
<appender-ref ref="AMQP" />
</root>
</configuration
6. Create a simple controller in the src\main\java
folder in your package:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@GetMapping("/hello")
public String hello(){
logger.info("***********Start Logging to rabbitMQ************");
logger.info("Publish Welcome Message to RabbitMQ");
return "Hello Shafique!";
}
}
OK, that is all we need to do. Let;s test the application.
7. Verify the log message in RabbitMQ Server's console.
Go to the Queue tab -> click on the queue name and the getMessages button.
Redelivered | ● | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Properties |
|
||||||||||||||||||||
Payload158 bytesEncoding: string |
|
You will see the following message, what we wrote in our controller hello()
method: Hello Shafique!
Opinions expressed by DZone contributors are their own.
Comments