Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating a Logging Application in RabbitMQ With Spring Boot and Logback

DZone's Guide to

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.

· Microservices Zone ·
Free Resource

Record growth in microservices is disrupting the operational landscape. Read the Global Microservices Trends report to learn more.

Since 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:

  1. Console Appender -> for console logging.

  2. File Appender -> for logging in a file.

  3. 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
app_id: AmqpAppenderTest
timestamp: 1546495455
message_id: d2ed43be-c5d4-421b-a6b4-b27e1973a1a9
priority: 0
delivery_mode: 1
headers:
categoryName: com.shafique.springbootlogbackrabbitmq.controller.HomeController
level: INFO
thread: http-nio-8080-exec-7
content_type: text/plain
Payload158 bytesEncoding: string


2019-01-03 11:34:15,437 INFO http-nio-8080-exec-7 
  [com.shafique.springbootlogbackrabbitmq.controller.HomeController] - 
  <Publish Welcome Message to RabbitMQ>


You will see the following message, what we wrote in our controller hello() method: Hello Shafique!

Learn why microservices are breaking traditional APM tools that were built for monoliths.

Topics:
rabbitmq tutorial ,microservices ,microservices tutorial ,spring boot tutorial ,spring boot microservices

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}