Backend Development 8 min read

Building a Lightweight Java Logging System with Loki, Loki4j, Grafana, and Spring Boot

This tutorial explains how to set up a simple yet powerful Java logging solution by installing Loki and Grafana via Docker, configuring Loki4j in a Spring Boot project, and visualizing logs in Grafana, providing step‑by‑step instructions and sample code.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Building a Lightweight Java Logging System with Loki, Loki4j, Grafana, and Spring Boot

In software development, a logging system is essential for debugging and monitoring applications in production. This article demonstrates how to create a lightweight, easy‑to‑use Java logging system using Loki, Loki4j, Grafana, and Spring Boot.

System Component Overview

What is Loki?

Loki, an open‑source log aggregation system from Grafana Labs, offers high availability, scalability, and multi‑tenancy. Inspired by Prometheus, it tags logs with labels and stores compressed log data, indexing only metadata to reduce storage costs and improve query performance.

What is Loki4j?

Loki4j is a Java‑specific log appender that sends logs directly to a Loki server. It integrates seamlessly with Spring Boot applications using Logback or Log4j2, providing a straightforward way for Java developers to manage logs.

What is Grafana?

Grafana is a powerful open‑source visualization and monitoring platform that supports multiple data sources, including Loki. It enables users to build dynamic, customizable dashboards to display log data intuitively.

Setup Steps

1. Install Loki and Grafana

Use Docker to quickly deploy both services:

# Install Loki
docker run -d --name loki -p 3100:3100 grafana/loki:latest -config.file=/etc/loki/local-config.yaml

# Install Grafana
docker run -d --name grafana -p 3000:3000 grafana/grafana:latest

2. Configure Loki4j

Add the Loki4j dependency to your pom.xml :

<dependency>
    <groupId>com.github.loki4j</groupId>
    <artifactId>loki-logback-appender</artifactId>
    <version>1.4.1</version>
</dependency>

Then create a logback.xml file under src/main/resources with the following configuration:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Spring application name -->
    <springProperty name="name" source="spring.application.name"/>

    <!-- Console appender -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [thread %thread] %-5level %logger{35}.%method - %msg%n</pattern>
        </encoder>
    </appender>

    <!-- Loki appender -->
    <appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
        <http>
            <url>http://localhost:3100/loki/api/v1/push</url>
        </http>
        <format>
            <label>
                <pattern>application=${name},level=%level</pattern>
                <readMarkers>true</readMarkers>
            </label>
            <message>
                <pattern>
                {"timestamp": "%d{yyyy-MM-dd HH:mm:ss.SSS}", "level": "%level", "logger": "%logger{36}.%method", "method":"%M", "line":"%line", "thread": "%thread", "message": "%msg%n"}
                </pattern>
            </message>
            <sortByTime>true</sortByTime>
        </format>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="LOKI"/>
    </root>
</configuration>

3. Configure Grafana

Open Grafana at http://localhost:3000 (default credentials: admin / admin), change the password, add Loki as a data source, and point it to the running Loki service.

4. Run the Spring Boot Application

Start your Spring Boot project; all logs will now be sent to Loki.

5. View Logs in Grafana

Create a dashboard in Grafana, select Loki as the data source, and use Loki’s query language to filter and display logs. For a quick view, go to the Explore page, choose Loki, set the label filter application=muqing‑api , and run the query.

Conclusion

By following these steps, you have built a simple Java logging system that leverages Loki’s aggregation capabilities, Loki4j’s easy integration, and Grafana’s visualization features. While this guide covers the basics, production environments may require additional considerations such as security and scaling. Future articles will explore Promtail, another Loki log collector with greater flexibility.

backendJavaDockerloggingSpring BootGrafanaLoki
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.