Operations 14 min read

How to Build a Lightweight Loki Logging Stack with Promtail and Grafana

This guide walks you through setting up a SpringBoot application, configuring Logback, installing and configuring Loki, Promtail, and Grafana, and comparing the lightweight Loki stack with the traditional ELK solution for efficient log collection and visualization.

Lobster Programming
Lobster Programming
Lobster Programming
How to Build a Lightweight Loki Logging Stack with Promtail and Grafana

Why Choose Loki Over ELK

ELK is a classic log analysis platform with rich features and strong community support, but it consumes a lot of resources, incurs high costs, and requires multiple components to be deployed and maintained, leading to high system complexity.

In contrast, Loki is lightweight because it does not index log content, only tags, resulting in far lower resource consumption. The following sections detail how to build a Loki‑based logging system.

1.1 Build the SpringBoot Project (PLG Process)

Project Structure

The SpringBoot project includes the following Maven dependencies:

<code>&lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
        &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;mysql&lt;/groupId&gt;
        &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;org.mybatis.spring.boot&lt;/groupId&gt;
        &lt;artifactId&gt;mybatis-spring-boot-starter&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
        &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
    &lt;/dependency&gt;
    
    &lt;dependency&gt;
        &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
        &lt;artifactId&gt;lombok&lt;/artifactId&gt;
    &lt;/dependency&gt;
&lt;/dependencies&gt;</code>

Logback Configuration (logback‑spring.xml)

<code>server:
  port: 8081

spring:
  application:
    name: prometheus-demo
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/longxia?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT+8
    password: root

mybatis:
  mapper-locations: classpath:/mapper/*.xml
  type-aliases-package: com.xiaowen.cloud.entity
  configuration:
    map-underscore-to-camel-case: true

# Logback appenders for console and rotating files (DEBUG, INFO, WARN, ERROR) with size‑time based rolling policies, UTF‑8 charset, and level filters.</code>

Controller Example

<code>@RestController
@RequestMapping("/test")
@Slf4j
public class OrderController {
    @GetMapping("/test1")
    public String test1(){
        log.info("ceshish info===========================");
        return "success";
    }
    @GetMapping("/test2")
    public String test2(){
        log.error("ceshish error===========================");
        return "success";
    }
    @GetMapping("/test3")
    public String test3(){
        log.warn("ceshishi  warn===========================");
        return "success";
    }
    @GetMapping("/test4")
    public String test4(){
        log.debug("ceshishi  debug===========================");
        return "success";
    }
    @GetMapping("/test5")
    public String test5(){
        int i = 1/0; // trigger exception
        return "success";
    }
}</code>

Package the project with mvn clean package , locate the generated JAR, and upload it to the Linux server.

Run the application in the background:

<code>nohup java -jar prometheus-demo.jar &amp;</code>

After invoking the controller endpoints, logs are written to the configured files.

1.2 Set Up Promtail

Download and install the RPM package:

<code>wget https://github.com/grafana/loki/releases/download/v3.2.1/promtail-3.2.1.x86_64.rpm
rpm -ivh promtail-3.2.1.x86_64.rpm</code>

Create /etc/promtail/config.yml with the following content (simplified):

<code>server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://192.168.174.128:3100/loki/api/v1/push

scrape_configs:
  - job_name: system
    static_configs:
      - targets:
          - localhost
        labels:
          job: spring_log_error
          __path__: /logs/prometheus-demo/error.log</code>

Grant Promtail read access to the log files:

<code>setfacl -m u:promtail:r /logs/prometheus-demo/error.log</code>

Start Promtail (either with nohup or systemctl ).

Verify that Promtail is scraping targets by visiting http://192.168.174.128:9080/targets .

1.3 Install Loki

Download and install the Loki RPM:

<code>wget https://github.com/grafana/loki/releases/download/v3.2.1/loki-3.2.1.x86_64.rpm
rpm -ivh loki-3.2.1.x86_64.rpm</code>

Start Loki:

<code>nohup /usr/bin/loki -config.file /etc/loki/config.yml &amp;</code>

Test Loki by opening http://192.168.174.128:3100/metrics in a browser.

1.4 Install Grafana

Download and install Grafana:

<code>wget https://dl.grafana.com/oss/release/grafana-6.6.1-1.x86_64.rpm
yum localinstall grafana-6.6.1-1.x86_64.rpm</code>

Start the Grafana service:

<code>systemctl restart grafana-server</code>

Access Grafana at http://192.168.174.128:3000/ (default credentials admin/admin).

1.5 Configure Grafana to Use Loki

Add a new data source of type “Loki” with the URL http://192.168.174.128:3100 . After saving, you can query logs using Loki’s query language, select the desired log files, and view results directly in Grafana.

2. ELK vs. Loki – When to Choose Which

ELK provides powerful full‑text search, complex queries, and multi‑source support, making it suitable for large systems that require deep analysis.

Loki, with its tag‑only indexing, offers much lower storage and compute costs and integrates seamlessly with Prometheus, making it ideal for high‑volume, simple‑query scenarios such as Kubernetes clusters.

If your environment is complex and you need advanced analytics, ELK remains the first choice; for lightweight, scalable logging, consider Loki.

ObservabilityloggingSpringBootELKGrafanaLokiPromtail
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

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.