Log4j 2.x

The sentry-log4j2 library provides Log4j 2.x support for Sentry via an Appender that sends logged exceptions to Sentry.

The source can be found on Github.

Note: raven-log4j2 is no longer maintained. It is highly recommended that you migrate to sentry-log4j2 (which this documentation covers). If you are still using raven-log4j2 you can find the old documentation here.


Using Maven:


Using Gradle:

compile 'io.sentry:sentry-log4j2:1.2.1'

Using SBT:

libraryDependencies += "io.sentry" % "sentry-log4j2" % "1.2.1"

For other dependency managers see the central Maven repository.


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 WARN level. The ConsoleAppender is only provided as an example of a non-Sentry appender that is set to a different logging threshold, like one you may already have in your project.

Example configuration using the log4j2.xml format:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn" packages="org.apache.logging.log4j.core,io.sentry.log4j2">
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />

        <Sentry name="Sentry" />

        <root level="INFO">
            <appender-ref ref="Console" />
            <!-- Note that the Sentry logging threshold is overridden to the WARN level -->
            <appender-ref ref="Sentry" level="WARN" />

Next, you’ll need to configure your DSN (client key) and optionally other values such as environment and release. See the configuration page for ways you can do this.

Additional Data

It’s possible to add extra data to events thanks to the marker system provided by Log4j 2.x.

Mapped Tags

By default all MDC parameters are stored under the “Additional Data” tab in Sentry. By specifying the extratags option in your configuration you can choose which MDC keys to send as tags instead, which allows them to be used as filters within the Sentry UI.

void logWithExtras() {
    // MDC extras
    MDC.put("Environment", "Development");
    MDC.put("OS", "Linux");

    // This sends an event where the Environment and OS MDC values are set as additional data
    logger.error("This is a test");

In Practice

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.MarkerManager;

public class MyClass {
    private static final Logger logger = LogManager.getLogger(MyClass.class);
    private static final Marker MARKER = MarkerManager.getMarker("myMarker");

    void logSimpleMessage() {
        // This sends a simple event to Sentry
        logger.error("This is a test");

    void logWithBreadcrumbs() {
        // Record a breadcrumb that will be sent with the next event(s),
        // by default the last 100 breadcrumbs are kept.
            new BreadcrumbBuilder().setMessage("User made an action").build()

        // This sends a simple event to Sentry
        logger.error("This is a test");

    void logWithTag() {
        // This sends an event with a tag named 'log4j2-Marker' to Sentry
        logger.error(MARKER, "This is a test");

    void logWithExtras() {
        // MDC extras
        ThreadContext.put("extra_key", "extra_value");
        // NDC extras are sent under 'log4j2-NDC'
        // This sends an event with extra data to Sentry
        logger.error("This is a test");

    void logException() {
        try {
        } catch (Exception e) {
            // This sends an exception event to Sentry
            logger.error("Exception caught", e);

    void unsafeMethod() {
        throw new UnsupportedOperationException("You shouldn't call this!");