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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

  • Creating a Web Project: Caching for Performance Optimization
  • Analyzing Techniques to Provision Access via IDAM Models During Emergency and Disaster Response
  • Navigating Change Management: A Guide for Engineers
  • How to Introduce a New API Quickly Using Micronaut
  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.6K 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!