Java logback logging

Intro

logback is “intended as a successor to the popular log4j project.”

Papertrail supports aggregating messages from a native logback appender, providing a live searchable console for your Java (JRE/JVM) app logs, including Scala apps using frameworks like Lift and Play. Here’s how.

Papertrail can receive logback messages using logback-syslog4j (com.papertrailapp.logback.Syslog4jAppender) or the older original logback ch.qos.logback.classic.net.SyslogAppender. As described below, we recommend logback-syslog4j.

Installation

Install logback

Install logback in your app or J2EE servlet container. This is basically adding the jars to your classpath, creating a logback.xml, and loading and invoking the classes to output messages (such as logger.debug("Success!");).

Choose appender

logback outputs logs to one or more appenders (such as file, console, or syslog). Papertrail recommends the logback-syslog4j appender (com.papertrailapp.logback.Syslog4jAppender) because it includes TCP, TCP with TLS, and Google App Engine support, as well as relying on syslog4j for message transmission.

If adding an appender to pom.xml is not possible, or you prefer the standard logback SyslogAppender, this appender can also can send to Papertrail.

Recommended: Syslog4jAppender

Visit logback-syslog4j README and follow the step-by-step instructions.

Important: In the logback.xml example in the README, replace host and port with the details from Papertrail’s Add Systems page. Replace ident with a name of your app, or other attribute which Papertrail should use to identify the sending app.

Customize sender name and identifier

To change the appearance of the sender name and program/calling method name in Papertrail:

<pattern>%d{MMM dd HH:mm:ss} HOSTNAME APPNAME: %-5level %logger{35}: %m%n%xEx</pattern>

HOSTNAME will appear as the sender name in Papertrail, and APPNAME will appear as the program.

Alternative: older SyslogAppender

Setup SyslogAppender for Papertrail

Edit logback.xml and add this within the <appenders> block.

Important: In the sample below, replace logsN and XXXXX with the details from Papertrail’s Add Systems page. Replace my-app with a name of your app, or other attribute which Papertrail should use to identify this sender.

<appender name="PAPERTRAIL" class="ch.qos.logback.classic.net.SyslogAppender">
  <syslogHost>logsN.papertrailapp.com</syslogHost>
  <port>XXXXX</port>
  <facility>USER</facility>
  <suffixPattern>my-app: %logger %msg</suffixPattern>  
</appender>

Enable SyslogAppender

Outside of the appenders section (in the <root> block), add this to enable the syslog appender you just created:

<appender-ref ref="PAPERTRAIL" />

An example might look like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} -
%msg%n</pattern>
    </encoder>
  </appender>

  <root level="DEBUG">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="PAPERTRAIL" />
  </root>
</configuration>

Formatting

Here’s an alternative suffixPattern which puts the %thread variable in the position that’s used for clickable context (instead of my-app above):

<suffixPattern>%thread: %-5level %logger{36} - %msg%n</suffixPattern>

Troubleshooting

I’m using the Spark Java Framework and no log lines are appearing in the event viewer

Spark and logback rely on SLF4J, so to avoid including it twice it must be excluded as a dependency.

Enable logback debug mode

logback supports a debug mode for its own operations. To enable it, change the outer <configuration> tag to <configuration debug="true">.

Read more