Observability - Logging

Logging

Log Aggregation

Grafana Loki

Loki - HTTP API Reference

Loki HTTP API (opens in a new tab)

Loki - Health check
GET :3100/ready
# e.g.
 http :3100/ready
HTTP/1.1 200 OK
Content-Length: 6
Content-Type: text/plain; charset=utf-8
Date: Fri, 03 Oct 2025 15:03:01 GMT
X-Content-Type-Options: nosniff
 
ready
Loki - Send log data to Loki

Send log data to Loki (opens in a new tab)

Tooling - LogQL simulator
Spring Boot - integration

Pusing logs directly over HTTP to Loki using a Logback Appender loki-logback-appender.

  1. Add dependencies (Gradle Kotlin DSL)
implementation("com.github.loki4j:loki-logback-appender:2.0.0")
  1. Configure Logback
<!-- logback-spring.xml -->
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
 
    <contextName>playground.spring-data</contextName>
 
    <springProperty name="logging.loki.url" source="logging.loki.url"/>
 
    <appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
        <http>
            <url>${logging.loki.url}</url>
        </http>
    </appender>
 
    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="LOKI"/>
    </root>
</configuration>
  1. Configure Spring Boot application properties
logging.loki.url=http://${LOKI_HOST:localhost}:${LOKI_PORT:3100}/loki/api/v1/push

VictoriaLogs

VictoriaLogs datasource for Grafana

Log Format

logfmt

JSON Lines

Logging API

SLF4J

SLF4J - Multiple bindings were found on the class path (opens in a new tab)
SLF4J - Modules
jul-to-slf4j
slf4j-jdk-platform-logging

Logback

Logback - Turn on debug flag
# System Property
-Dlogback.debug=true
# This will print out internal status messages of logback
Logback - MDC
  • Store contextual information on a per-thread basis, such as a transaction ID.
Logback - Config
Logback - Appenders
  • immediateFlush

    By default, each log event is immediately flushed to the underlying output stream. This default approach is safer in the sense that logging events are not lost in case your application exits without properly closing appenders.

    Try using immediateFlush = true if logging events are lost in logs.

Logback - RollingFileAppender
  • RollingPolicy

    • How to roll over log files
  • TriggeringPolicy

    • When to roll over log files
Logback - PatternLayout
Logback - Spring Boot - integration
  • Spring Boot by default uses programmatic configuration to improve startup time, with Spring config for customizations.

    Logback default config class: org.springframework.boot.logging.logback.DefaultLogbackConfiguration

Logback - Spring Boot - default config
org.springframework.boot.logging.logback