Over a million developers have joined DZone.

Getting Spring Boot to work with Papertrail logging

DZone's Guide to

Getting Spring Boot to work with Papertrail logging

· DevOps Zone
Free Resource

Download “The DevOps Journey - From Waterfall to Continuous Delivery” to learn learn about the importance of integrating automated testing into the DevOps workflow, brought to you in partnership with Sauce Labs.

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"?>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>

    <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
        <suffixPattern>${PAPERTRAIL_APP:-app} %highlight([%.-1level]) %35.35logger{35}:\t%m\t%cyan%ex{5}</suffixPattern>
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="SYSLOG" />
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;

public class Application {
    private static final Logger log = LoggerFactory.getLogger(Application.class);
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    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 

Discover how to optimize your DevOps workflows with our cloud-based automated testing infrastructure, brought to you in partnership with Sauce Labs


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}