Getting Spring Boot to work with Papertrail logging
Join the DZone community and get the full member experience.
Join For Freespring 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:
- create logging group in papetrail dashboard ("create group" button).
- create log destination in papertrail dashboard.
- go to "account -> log destinations"
- click "create log destination" button.
- make sure your group is selected in " new systems will join" field.
- you can leave all others fields with their default values, just click "create".
- 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>
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)
you can find sample project at github: https://github.com/bsideup/spring-boot-sample-papertrail
Opinions expressed by DZone contributors are their own.
Comments