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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Trending

  • The Perfection Trap: Rethinking Parkinson's Law for Modern Engineering Teams
  • The Role of AI in Identity and Access Management for Organizations
  • Navigating Change Management: A Guide for Engineers
  • Building a Real-Time Audio Transcription System With OpenAI’s Realtime API
  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.

By 
Mohammed Shafique user avatar
Mohammed Shafique
·
Jan. 07, 19 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
21.5K 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.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!