DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • 5 Layers of Prompt Injection Defense You Can Wire Into Any Node.js App
  • You Are Using Claude Wrong (And So Is Everyone You Know)
  • Building AI-Powered Java Applications With Jakarta EE and LangChain4j
  • Getting Started With Agentic Workflows in Java and Quarkus
  1. DZone
  2. Coding
  3. Frameworks
  4. Getting Spring Boot to work with Papertrail logging

Getting Spring Boot to work with Papertrail logging

By 
Sergei Egorov user avatar
Sergei Egorov
·
Jan. 09, 15 · Interview
Likes (2)
Comment
Save
Tweet
Share
8.8K Views

Join the DZone community and get the full member experience.

Join For Free

spring boot already comes with great, pre-configured logging system inside, but in real projects it's important to have an ability to search logs, aggregate them and access easy. one of the easiest option for it is http://papertrailapp.com/ . they provide logging service with syslog protocol and 100mb/mo free plan.

lets prepare papertrail for our example:

  1. create logging group in papetrail dashboard ("create group" button).
  2. create log destination in papertrail dashboard.
    1. go to "account -> log destinations"
    2. click "create log destination" button.
    3. make sure your group is selected in " new systems will join" field.
    4. you can leave all others fields with their default values, just click "create".
  3. remember your log destination (will looks like logs2.papertrailapp:12345), we will use it later

spring boot uses logback as default logging system. it's powerful tool for logging with many logging options. for our purposes we will use ch.qos.logback.classic.net.syslogappender .

add "logback.xml" file to your "resources" folder with following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <appender name="syslog" class="ch.qos.logback.classic.net.syslogappender">
        <sysloghost>${papertrail_host}</sysloghost>
        <port>${papertrail_port}</port>
        <facility>user</facility>
        <suffixpattern>${papertrail_app:-app} %highlight([%.-1level]) %35.35logger{35}:\t%m\t%cyan%ex{5}</suffixpattern>
        <throwableexcluded>true</throwableexcluded>
    </appender>
    
    <root level="info">
        <appender-ref ref="console" />
        <appender-ref ref="syslog" />
    </root>
    
</configuration>
i'm using environment variables ( (1), (2), and (3) ) for papertrail's credentials, it will allow you to configure different log destinations in different application environments.

note excluded throwable at (4). we already have pattern for throwable in suffixpattern ( %cyan%ex{5} ). why we do this? because otherwise exception stacktraces will be printed after main log message line-by-line, it will increase traffic and also you will not be able to see stacktrace in search.

i will demonstrate how it works with really simple spring boot application:

package com.github.bsideup.spring.boot.example.papertrail;

import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;

@springbootapplication
@restcontroller
public class application {
    
    private static final logger log = loggerfactory.getlogger(application.class);
    
    public static void main(string[] args) {
        springapplication.run(application.class, args);
    }
    
    @requestmapping("/")
    public string index() {
        log.warn("i'm so tired to welcome everyone", new exception(new exception()));
        return "hello world!";
    }
}
now, run your application with following environment variables:
  • papertrail_host - host from your log destination (i.e. logs2.papertrailapp.com)
  • papertrail_port - port from your log destination (i.e. 12345)
  • [optional] papertrail_app - application name (default: app)
your papertrails output will looks like mine:



you can find sample project at github: https://github.com/bsideup/spring-boot-sample-papertrail

Spring Framework Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook