Spring Boot Centralized Logging with Graylog
In this article, I’ll explain how to centralize the logs of your Spring Boot application with Graylog.
Join the DZone community and get the full member experience.Join For Free
Logs are an essential part of any system, they give you detailed information about your application, what your system is doing, and what caused the error if something went wrong.
Almost all systems generate logs in one form or another, these logs are written to files on local drives. Debugging the error in a production application through hundreds of log files and on hundreds of servers can be very time consuming and complicated. One approach to this problem is to create a centralized log management system that can collect and aggregate different types of logs in one location.
What is Graylog?
Graylog is a powerful platform that allows for easy log management of both structured and unstructured data along with debugging applications. It is based on Elasticsearch, MongoDB, and Scala. Graylog has a main server, which receives data from its clients installed on different servers, and a web interface, which visualizes the data and allows to work with logs aggregated by the main server.
Many installation methods are recommended in official documents. Here we are using docker compose to easily setup a graylog instance with data persistent support.
Start the graylog instance with docker-compose up -d
Sending Application Logs (Spring Boot Application)
After creating the Spring Boot application, add a logback-gelf dependency in the pom file.
We used Logback-gelf which is used here to generate logs in Graylog. There is a detailed introduction to using logback-gelf on here.
- logback.xml file
Receiving Logs (Graylog Server)
Access to the Graylog server in the browser http://ip:9000.
To send the data to Graylog, you must, therefore, configure an entry. This will tell Graylog to accept the log messages.
Go to the Web interface -> System -> Entries and select "GELF UDP" and click on "Launch a new entry".
Run your Spring Boot application. you can see the captured logs in the Graylog interface.
You can find the complete source code in my Github page.
Opinions expressed by DZone contributors are their own.