Logback Integration

Logback is a popular logging framework for Java. If you are already using Logback in your application, it is very easy to extend your logging configuration to automatically send all errors to the Codetrails Error Analytics service using our Logback client.

How to Send an Error Report

Once installed and configured, you can send a report to your service simply by logging an error:

package com.example.application;

import ch.qos.logback.classic.Logger;

public class Application {

  private static final Logger LOGGER = ...

  public void report() {
    LOGGER.error("Encountered an error");
  }
}

This will send an error report containing your message and a stack trace indicating where in your code you logged the error.

Of course, if you catch and log an exception, you can also send this stack trace along with your report:

public class Application {

  public void reportCaughtException() {
    try {
      ...
    } catch (Exception e) {
      LOGGER.error("Caught an exception", e);
    }
  }
}

How to Install the Client

If you are using Apache Maven, just add the following to the <dependencies> section of your POM:

pom.xml
<dependency>
  <groupId>com.ctrlflow.aer.client.bundles</groupId>
  <artifactId>com.ctrlflow.aer.client.logback</artifactId>
  <version>2.0.1</version>
</dependency>

This will automatically install the Codetrails Error Analytics Logback Client as well as any required dependencies from the Central Repository.

How to Configure the Client

Once you have installed the client, you have to make a few, simple additions to your Logback configuration. In particular, you have to configure the submission URL of your Codetrails Error Analytics service, which you can find on the Help > System Information page:

client rest api urls.png
Figure 1. Help > System Information > REST API URLs panel.

The listing below shows an example configuration, which will send all log events logged at a level of ERROR to your service’s submission URL:

logback.xml
<appender name="AER" class="com.ctrlflow.aer.client.logback.AerAppender">

  <!-- The submission URL of your Codetrails Error Analytics service. -->
  <url>https://aer.ctrlflow.com/example/community/new</url>

  <!-- Optional metadata to include in the error report. -->
  <userName>John Doe</userName>
  <email>john.doe@example.com</email>
  <productId>com.example.application</productId>
  <buildId>1.0.0</buildId>
</appender>

<appender name="ASYNC-AER" class="ch.qos.logback.classic.AsyncAppender">
  <appender-ref ref="AER"/>
  <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <level>ERROR</level>
   </filter>
   <neverBlock>true</neverBlock>
</appender>

<root ...>
  ...
  <appender-ref ref="ASYNC-AER"/>
</root>

Note that the above does not add the Codetrails Error Analytics appender itself to the root logger; instead, it wraps the appender inside another, asynchronous appender. This is crucial, as sending error reports to a remote server should not block the logging thread.

Once you have configured the client in your logback.xml, reporting errors is just logging as usual.

Tip
When building your product with Maven, you may want to use the built-in Maven properties ${project.artifactId} and ${project.version} for productId and buildId, respectively. Check out the Logback example on GitHub for details.

results matching ""

    No results matching ""