Better Java Logging with Loki and Grafana

The logging options through Render log streams while very easy to setup, unfortunately omit log levels and show everything as an ‘alert’, and overall feels a bit lacking.

If you’re here and using a Java application, I figured out a much better alternative with Loki and Grafana Cloud. The first 50GB of logging metrics is free making it more affordable than any of the partners Render has integrated and the setup is relatively straight forward taking only a bit of tinkering to get working. Plus! It will preserve your applications log formatting and log levels!

By simply adding a Maven dependency and a logback.xml file with a new appender I was able to push my logs to Grafana. More detailed setup guide here: Loki4j Logback · Pure Java Logback appender for Grafana Loki

Example logback.xml file which passes the Grafana API key, username and app name as environment variables. It uses the Spring profile to determine if you want to use the Loki appender - sending logs to Grafana, or a local Spring profile, sending logs to the local console.

Also no changes to the Dockerfile were needed.

<?xml version="1.0" encoding="UTF-8"?>
<property name="LOGS" value="./logs"/>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>

<appender name="LOKI" class="com.github.loki4j.logback.Loki4jAppender">
            <pattern>l=%level h=${APP_NAME} c=%logger{20} t=%thread | %msg %ex</pattern>

<springProfile name="sandbox">
    <root level="DEBUG">
        <appender-ref ref="LOKI"/>

<springProfile name="prod">
    <root level="DEBUG">
        <appender-ref ref="LOKI"/>

<springProfile name="local-prod">
    <root level="DEBUG">
        <appender-ref ref="STDOUT" />