Logback

Once this 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.

Don't already have an account and Sentry project established? Head over to sentry.io, then return to this page.

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

Copied
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-logback</artifactId>
    <version>8.0.0</version>
</dependency>

For other dependency managers, see the central Maven repository.

When running your application, please add our sentry-opentelemetry-agent to the java command. You can download the latest version of the sentry-opentelemetry-agent-8.0.0.jar from MavenCentral. It's also available as a ZIP containing the JAR used on this page on GitHub.

Copied
SENTRY_PROPERTIES_FILE=sentry.properties java -javaagent:sentry-opentelemetry-agent-8.0.0.jar -jar your-application.jar

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

The following example configures 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
<configuration>
    <!-- Configure the Console appender -->
    <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>

    <!-- Configure the Sentry appender, overriding the logging threshold to the WARN level -->
    <appender name="Sentry" class="io.sentry.logback.SentryAppender" />

    <!-- Enable the Console and Sentry appenders, Console is provided as an example
 of a non-Sentry logger that is set to a different logging threshold -->
    <root level="INFO">
        <appender-ref ref="Console" />
        <appender-ref ref="Sentry" />
    </root>
</configuration>

Two log levels are used to configure this integration:

  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 which 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 sent 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
<appender name="Sentry" class="io.sentry.logback.SentryAppender">
    <!-- Optionally change minimum Event level. Default for Events is ERROR -->
    <minimumEventLevel>WARN</minimumEventLevel>
    <!-- Optionally change minimum Breadcrumbs level. Default for Breadcrumbs is INFO -->
    <minimumBreadcrumbLevel>DEBUG</minimumBreadcrumbLevel>
</appender>

The SDK can be configured using a sentry.properties file:

sentry.properties
Copied
dsn=https://examplePublicKey@o0.ingest.sentry.io/0
traces-sample-rate=1.0

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

Copied
import io.sentry.Sentry;

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

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

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").