DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Creating a Logging Application in RabbitMQ With Spring Boot and Logback

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.

Mohammed Shafique user avatar by
Mohammed Shafique
·
Jan. 07, 19 · Tutorial
Like (10)
Save
Tweet
Share
18.84K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Spring Framework application Spring Boot

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top 10 Secure Coding Practices Every Developer Should Know
  • Load Balancing Pattern
  • 5 Factors When Selecting a Database
  • How Observability Is Redefining Developer Roles

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: