Log4j 2.x

Once the integration is configured you can also use Sentry’s static API, as shown on the usage page, in order to do things like record breadcrumbs, set the current user, or manually send events. The source can be found on GitHub.

On this page, we get you up and running with Sentry's SDK, so that it will automatically report errors and exceptions in your application.

Install

Sentry captures data by using an SDK within your application’s runtime.

Copied
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-log4j2</artifactId>
    <version>3.2.0</version>
</dependency>

For other dependency managers see the central Maven repository.

Configure

Configuration should happen as early as possible in your application's lifecycle.

The following example using the log4j2.xml format to configure a ConsoleAppender that logs to standard out at the INFO level, and a SentryAppender that logs to the Sentry server at the ERROR level.

Copied
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <Sentry name="Sentry"
                dsn="https://examplePublicKey@o0.ingest.sentry.io/0" />
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Sentry"/>
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

DSN Configuration

Note that you need to configure your DSN (client key) only if you wish to initialize the SDK through the log4j2 integration. If you're planning to use Sentry.init to provide configuration, such as by using the beforeSend callback, you should not provide the DSN in both Sentry.init and the appender configuration; just leave it out of the appender configuration in this case.

Copied
<Sentry name="Sentry"
        dsn="https://examplePublicKey@o0.ingest.sentry.io/0" />

If the DSN is not present in the log4j2.xml configuration, Sentry will attempt to read it from the system property sentry.dsn, environment variable SENTRY_DSN or the dsn property in sentry.properties file. See the configuration page for more details on external configuration.

Minimum Log Level

Two log levels are used to configure this integration, as illustrated below in the provided code samples:

  1. Configure the lowest level required for a log message to become an event (minimumEventLevel) sent to Sentry.
  2. Configure the lowest level a message has to be to become a breadcrumb (minimumBreadcrumbLevel)

Breadcrumbs are kept in memory (by default the last 100 records) and are sent with events. For example, by default, if you log 100 entries with logger.info or logger.warn, no event is sent to Sentry. If you then log with logger.error, an event is sent to Sentry that includes those 100 info or warn messages. For this to work, SentryAppender needs to receive all log entries to decide what to keep as breadcrumb or send as event. Set the SentryAppender log level configuration to a value lower than what is set for the minimumBreadcrumbLevel and minimumEventLevel so that SentryAppender receives these log messages.

Copied
<!-- Setting minimumBreadcrumbLevel modifies the default minimum level to add breadcrumbs from INFO to DEBUG  -->
<!-- Setting minimumEventLevel the default minimum level to capture an event from ERROR to WARN  -->
<Sentry name="Sentry"
        dsn="https://examplePublicKey@o0.ingest.sentry.io/0"
        minimumBreadcrumbLevel="DEBUG"
        minimumEventLevel="WARN"
/>

Verify

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:

Copied
import java.lang.Exception;
import io.sentry.Sentry;

try {
    throw new Exception("This is a test.");
} catch (Exception e) {
    Sentry.captureException(e);
}

Or, by manually generating an event:

In Java you can capture any exception object that you caught:

Copied
import io.sentry.Sentry;

try {
  aMethodThatMightFail();
} catch(Exception e) {
  Sentry.captureException(e);
}

To view and resolve the recorded error, log into sentry.io and open your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

You can edit this page on GitHub.