Better Stack Java logging

Start logging in 5 minutes

Collect logs from your Java applications using Logback.

1. Dependencies

Add Better Stack Logback appender to your pom.xml or build.gradle file:

Maven (pom.xml) Gradle (build.gradle)
<dependency>
    <groupId>com.logtail</groupId>
    <artifactId>logback-logtail</artifactId>
    <version>0.4.0</version>
</dependency>
dependencies {
    // ...existing dependencies...
    implementation 'com.logtail:logback-logtail:0.4.0'
}

You'll also need Logback. Spring Boot and the Quarkus quarkus-logging-logback extension already include it and manage its version.

Don't have Logback in your project yet?
Add logback-classic to your pom.xml or build.gradle file. It brings logback-core and slf4j-api along:

Maven (pom.xml) Gradle (build.gradle)
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.5.38</version>
</dependency>
dependencies {
    // ...existing dependencies...
    implementation 'ch.qos.logback:logback-classic:1.5.38'
}

Logback 1.5 needs Java 11 or newer. On Java 8, use Logback 1.3, for example 1.3.16.

2. Setup

Set up log appenders in your logback.xml configuration:

Logback config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="Logtail" class="com.logtail.logback.LogtailAppender">
        <appName>MyApp</appName>
        <sourceToken>$SOURCE_TOKEN</sourceToken>
        <ingestUrl>https://$INGESTING_HOST</ingestUrl >
        <mdcFields>requestId,requestTime</mdcFields>
        <mdcTypes>string,int</mdcTypes>
    </appender>
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %msg%n</pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="Logtail" />
        <appender-ref ref="Console" />
    </root>
</configuration>


3. Start logging πŸŽ‰

Import and use the logger:

Send logs to Logtail
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class App {
    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(App.class);
        logger.error("Something bad happened.");
        String json = "{ \"item\": \"Orange soda\", \"price\": 100.00 }";
        logger.info("Log message with structured logging " + json);
    }
}

You should see your logs in Better Stack β†’ Live tail.

Find structured data under the message_json field.
Better Stack parses any JSON at the end of the log line.

Need help?

Please let us know at hello@betterstack.com.
We're happy to help! πŸ™

Additional information

  • New to logging in Java? Check out our Intro guide to logging in Java.
  • Want to support more data types? Install an add-on module for Jackson and register it in the Logtail appender's <objectMapperModule> tag. For example, see adding JavaTimeModule on Github. Keep all Jackson artifacts on one version, for example by importing com.fasterxml.jackson:jackson-bom, or the appender fails to start with NoClassDefFoundError.
  • Running a short-lived process? Logs still queued when the JVM exits are sent before it shuts down, for up to maxFlushTime, which is 30 seconds by default.

Configure the appender

Fine-tune logging with more options inside the <appender name="Logtail"> tag in logback.xml. The appender ignores out-of-range values with a warning that Logback prints at startup.

Appender options

sourceToken
required string
ingestUrl
https://in.logs.betterstack.com string
appName
string
mdcFields
string
mdcTypes
string
maxQueueSize
100000 integer
batchSize
1000 integer
batchInterval
3000 integer
connectTimeout
5000 integer
readTimeout
10000 integer
maxRetries
5 integer
retrySleepMilliseconds
300 integer
maxFlushTime
30000 integer
objectMapperModule
string

Example project

Want to try a more detailed example with Maven or Gradle?
See our Java Logtail example projects on GitHub.