Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Spring Boot to work with Papertrail logging

DZone's Guide to

Getting Spring Boot to work with Papertrail logging

· DevOps Zone ·
Free Resource

Learn more about how CareerBuilder was able to resolve customer issues 5x faster by using Scalyr, the fastest log management tool on the market. 

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 

Find out more about how Scalyr built a proprietary database that does not use text indexing for their log management tool.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}