RUN@cloud » Logging with Logback and Papertrail

Logging with Logback and Papertrail

Last modified by Troy Davis on 2012/07/30 01:05

Here's how to add the logback logging framework to your app on CloudBees, send the messages to Papertrail's log management service, and customize the verbosity for different app components.

We'll Create an app, Setup logback, and Customize logging.

Obligatory candy

We love us some pretty pictures:

Hello World log messages in Papertrail viewer

What's unique here?

We'll add these capabilities to your app's logging:

  • Realtime tail and search from the Web and command-line
  • Jump right to a timestamp where an error occurred, like for troubleshooting a support ticket or exception
  • Adjust per-component log verbosity by changing logback.xml
  • Receive important logs in a nightly email or as monitoring alerts every minute

.. and a few other things, all to help the app tell you what's happening and why.

Although log4j gets more press, logback is often called its successor (background, differences). If you have a log4j.properties file, convert it.

Create an app

Let's get down to business. If you already have an app on CloudBees, skip to Setup logback.

Here's how to create and deploy a new J2EE app on CloudBees. We'll create a skeleton Hello World app that also logs Hello World via logback.

1. Sign up

Sign up for CloudBees and subscribe to 2 free subscriptions: RUN@Cloud and Papertrail.

2. Setup app

On run.cloudbees.com, hit Add New Application, name it Hello World, and accept the defaults (details in Getting Started). You'll see a confirmation:

Your new application is now running at: http://helloworld.houseofawesome.cloudbees.net##

Click the Configuration tab and enable Papertrail:

CloudBees Configuration tab - Papertrail

and Save that change:

CloudBees Configuration tab - Save

3. Install SDK

Install the CloudBees SDK - basically download, unzip, and set 2 variables. To verify that the SDK works, run this. Change the app ID to the one shown on your app's Configuration tab:

bees app:info -a houseofawesome/helloworld

You should see:

# CloudBees SDK version: 0.7.3
You have not created a CloudBees configuration profile, let's create one now...
Enter your CloudBees account email address: your-cloudbees-account-email@example.com
Enter your CloudBees account password:
Application     : houseofawesome/helloworld
  .. snipped ..

Create a new skeleton app per CloudBees' Maven Guide. Run its mvn archetype:create command, then merge its 2 pom.xml stanzas into your new myapp/pom.xml.

We've now got an app with CloudBees deployment tools. To see it locally, run bees app:run and browse to http://localhost:8080.

Setup logback

Now we'll add the logback dependencies, configure logback, add a logging call, and deploy the app.

1. Add logback

Edit pom.xml again and add these to <dependencies>:

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>

2. Configure logback

In myapp/src/main/resources/ (which should already exist), create a new file called logback.xml with:

<configuration>
 <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
   <encoder>
     <pattern>[%thread] %-5level %logger{36} - %msg%n</pattern>
   </encoder>
 </appender>

 <logger name="myapp" level="TRACE"/>

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

This creates one logger we've called myapp, and sends output from all loggers to STDOUT at debug verbosity. Since your app uses the Papertrail subscription, CloudBees will automatically send standard output to Papertrail as it occurs. The <pattern> specifies a basic format.

3. Log

In a typical servlet, use logback with:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

final static Logger logger = LoggerFactory.getLogger(MyApp1.class);

Then generate a message at debug verbosity by calling:

logger.debug("Entering application.");

Obviously in production apps, keep logging in controllers and libraries. Since our skeleton app only has a single JSP, we'll change it to render "Hello World" in the browser and also output that to logback.

Edit myapp/src/main/webapp/index.jsp and insert these 4 lines anywhere:

<%@ page import="org.slf4j.Logger" %>
<%@ page import="org.slf4j.LoggerFactory" %>
<% Logger logger = LoggerFactory.getLogger("myapp"); %>
<% logger.info("Hello World!"); %>

Re-run mvn bees:run and reload http://localhost:8080. A new log line will appear each time you reload:

[http-8080-exec-1] INFO  myapp - Hello World!

Deploy

Once it's working locally, load Papertrail on CloudBees to see logs in realtime, and run: 

mvn bees:deploy

.. to deploy the app. You'll see log messages appear as the app restarts. Then browse to your app's root URL; each page load should immediately generate a new log message in Papertrail's viewer.

"Hello World" screenshot

Customize logging

Your app now has an easy way to describe state changes, unexpected conditions, object properties, and potential problems, and it's all visible and searchable as it happens. Here's a few things to customize or try:

Logger name

Change myapp in logback.xml and in the getLogger call.

Output format

Troubleshooting? Check out the %caller{2} format option to include a 2-line mini-stack trace with line numbers. Change the <pattern> using these format specifiers.

Per-component verbosity

Create multiple loggers and adjust the verbosity for each.

In an app with multiple components, one logger per component makes it easy to adjust the verbosity of only the one you're investigating (and puts the component/logger name in the message). Just add a logger entry to logback.xml and change the getLogger argument.

Even class-level verbosity is possible, like: LoggerFactory.getLogger(Foo.class);. Here's more.

Search and react

Save a Papertrail search for something important. For example, try "Stopping Coyote" to see all restarts. Then save that search and create an alert to receive immediate pages or nightly emails:

Papertrail time search

Command-line access

Install Papertrail's command-line client: sudo gem install papertrail. More.

Tags: logging
Created by Troy Davis on 2011/11/12 00:55