Deploy Loki & Grafana for Lightweight Log Monitoring with Docker and SpringBoot
This tutorial walks through setting up the lightweight Loki log aggregation system together with Grafana using Docker Compose, integrating Loki into a SpringBoot application via the loki‑logback‑appender, and configuring Grafana dashboards to visualize distributed logs on a low‑memory server.
After a failed attempt to run SkyWalking on a low‑memory 2 GB server, the author switched to the lightweight Loki log aggregation system combined with Grafana for visualization, and provides a step‑by‑step guide to deploy both services with Docker Compose, integrate Loki into a SpringBoot application via the loki‑logback‑appender, and configure Grafana to display logs.
Environment Deployment
The setup consists of two components, Loki and Grafana, which together consume less than 1 GB of memory when run in Docker.
version: "3"
networks:
loki:
services:
loki:
image: grafana/loki:latest
ports:
- "3100:3100"
command: -config.file=/etc/loki/local-config.yaml
networks:
- loki
grafana:
environment:
- GF_PATHS_PROVISIONING=/etc/grafana/provisioning
- GF_AUTH_ANONYMOUS_ENABLED=true
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
entrypoint:
- sh
- -euc
- |
mkdir -p /etc/grafana/provisioning/datasources
cat <<EOF > /etc/grafana/provisioning/datasources/ds.yaml
apiVersion: 1
datasources:
- name: Loki
type: loki
access: proxy
orgId: 1
url: http://loki:3100
basicAuth: false
isDefault: true
version: 1
editable: false
EOF
/run.sh
image: grafana/grafana:latest
ports:
- "3000:3000"
networks:
- lokiRun the following command in the directory containing docker-compose.yaml to pull images and start the containers: docker-compose -f docker-compose.yaml up Verify the containers are running with:
docker psSpringBoot Integration with Loki
Integrate Loki by adding the loki-logback-appender-jdk8 dependency to your project:
<dependency>
<groupId>com.github.loki4j</groupId>
<artifactId>loki-logback-appender-jdk8</artifactId>
<version>1.4.2</version>
</dependency>Configure logback-spring.xml to use the Loki appender (excerpt):
<!-- Loki log upload -->
<springProperty name="serverIP" scope="context" source="spring.cloud.client.ip-address" defaultValue="0.0.0.0"/>
<springProperty name="serverPort" scope="context" source="server.port" defaultValue="0000"/>
<springProperty name="appName" scope="context" source="spring.application.name"/>
<springProperty name="lokiUrl" scope="context" source="loki.url"/>
<property name="APP_NAME" value="${appName}"/>
<property name="LOKI_URL" value="${lokiUrl}"/>
<appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
<http class="com.github.loki4j.logback.ApacheHttpSender">
<url>${LOKI_URL}/loki/api/v1/push</url>
</http>
<format>
<label>
<pattern>app=${APP_NAME},host=${HOSTNAME},instance=${serverIP}:${serverPort}, level=%level</pattern>
</label>
<message>
<pattern>l=%level h=${HOSTNAME} i=${serverIP}:${serverPort} c=%logger{20} t=%thread | %msg %ex</pattern>
</message>
<sortByTime>true</sortByTime>
</format>
</appender>
<root level="INFO">
<appender-ref ref="LOKI"/>
</root>Add the Loki endpoint in application.yml (replace with your server IP):
# Loki configuration
loki:
url: http://192.168.11.11:3100All logs emitted via Logback (e.g., log.error, log.info) will be sent to Loki and become searchable.
Grafana Visualization of Loki
Access Grafana at http://<em>server_ip</em>:3000/login (default credentials: admin / admin). After logging in, add a new data source:
Select Loki, set the URL to http://<em>container_name</em>:3100, and test the connection.
Once the data source is verified, create a dashboard and use a simple query such as {app="appName"} (where appName matches spring.application.name) to view logs from the SpringBoot service.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
