Java

Installation

Gradle

implementation 'io.airbrake:javabrake:0.2.4'

Maven

<dependency>
  <groupId>io.airbrake</groupId>
  <artifactId>javabrake</artifactId>
  <version>0.2.4</version>
</dependency>

Ivy

<dependency org='io.airbrake' name='javabrake' rev='0.2.4'>
  <artifact name='javabrake' ext='pom'></artifact>
</dependency>

Configuration

import io.airbrake.javabrake.Notifier;
import io.airbrake.javabrake.Config;

Config config = new Config();
config.projectId = 12345;
config.projectKey = "FIXME";
Notifier notifier = new Notifier(config);

notifier.addFilter(
    (Notice notice) -> {
      notice.setContext("environment", "production");
      return notice;
    });

By default report sends errors asynchronously returning a Future, but synchronous API is also available:

import io.airbrake.javabrake.Notice;

Notice notice = Airbrake.reportSync(e);
if (notice.exception != null) {
    logger.info(notice.exception);
} else {
    logger.info(notice.id);
}

To set custom params you can build and send notice in separate steps:

import io.airbrake.javabrake.Notice;

Notice notice = Airbrake.buildNotice(e);
notice.setContext("component", "mycomponent");
notice.setParam("param1", "value1");
Airbrake.send(notice);

You can also set custom params on all reported notices:

notifier.addFilter(
    (Notice notice) -> {
      notice.setParam("myparam", "myvalue");
      return notice;
    });

Or ignore specific notice:

notifier.addFilter(
    (Notice notice) -> {
      if (notice.context.get("environment") == "development") {
          // Ignore notice.
          return null;
      }
      return notice;
    });

To debug why notices are not sent you can use onReportedNotice hook:

notifier.onReportedNotice(
    (notice) -> {
      if (notice.exception != null) {
        logger.info(notice.exception);
      } else {
        logger.info(String.format("notice id=%s url=%s", notice.id, notice.url));
      }
    });

HTTP proxy

javabrake uses OkHttp as an HTTP client. So in order to use proxy all you have to do is to configure OkHttpClient:

import java.net.InetSocketAddress;

import okhttp3.OkHttpClient;
import okhttp3.Proxy;

import io.airbrake.javabrake.OkSender;

Proxy proxy = new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved("192.168.1.105", 8081);
OkHttpClient httpClient =
    new OkHttpClient.Builder()
        .connectTimeout(3000, TimeUnit.MILLISECONDS)
        .readTimeout(3000, TimeUnit.MILLISECONDS)
        .writeTimeout(3000, TimeUnit.MILLISECONDS)
        .proxy(proxy)
        .build();
OkSender.setOkHttpClient(httpClient);

Usage

Using notifier directly

try {
  do();
} catch (IOException e) {
  notifier.report(e);
}

Using Airbrake proxy class

import io.airbrake.javabrake.Airbrake;

try {
  do();
} catch (IOException e) {
  Airbrake.report(e);
}

log4j2 integration

Installation

Gradle
compile 'io.airbrake:log4javabrake2:0.1.8'
Maven
<dependency>
  <groupId>io.airbrake</groupId>
  <artifactId>log4javabrake2</artifactId>
  <version>0.1.8</version>
</dependency>
Ivy
<dependency org='io.airbrake' name='log4javabrake2' rev='0.1.8'>
  <artifact name='log4javabrake2' ext='pom'></artifact>
</dependency>

Configuration

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" packages="io.airbrake.log4javabrake2">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <Airbrake name="Airbrake" projectId="12345" projectKey="FIXME" env="production"></Airbrake>
  </Appenders>
  <Loggers>
    <Root>
      <AppenderRef ref="Console"/>
      <AppenderRef ref="Airbrake"/>
    </Root>
  </Loggers>
</Configuration>

logback integration

Installation

Gradle
compile 'io.airbrake:logback:0.1.1'
Maven
<dependency>
  <groupId>io.airbrake</groupId>
  <artifactId>logback</artifactId>
  <version>0.1.1</version>
</dependency>
Ivy
<dependency org='io.airbrake' name='logback' rev='0.1.1'>
  <artifact name='logback' ext='pom'></artifact>
</dependency>

Configuration

<configuration>
  <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="Airbrake" class="io.airbrake.logback.AirbrakeAppender">
    <projectId>12345</projectId>
    <projectKey>FIXME</projectKey>
    <env>production</env>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>ERROR</level>
    </filter>
  </appender>

  <root level="INFO">
    <appender-ref ref="Console" />
    <appender-ref ref="Airbrake" />
  </root>
</configuration>