---
title: "log4j 1.x"
url: https://docs.sentry.io/platforms/java/guides/log4j2/legacy/log4j/
---

# log4j 1.x | Sentry for Log4j 2.x

##### Note

An [updated Java SDK](https://docs.sentry.io/platforms/java.md) supersedes this deprecated version. Sentry preserves this documentation for customers using the old client. We recommend using the updated Java SDK for new projects.

The `sentry-log4j` library provides [Log4j 1.x](https://logging.apache.org/log4j/1.2/) support for Sentry via an [Appender](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Appender.html) that sends logged exceptions to Sentry. Once this integration is configured you can *also* use Sentry’s static API, [as shown on the usage page](https://docs.sentry.io/platforms/java/legacy/usage.md), in order to do things like record breadcrumbs, set the current user, or manually send events.

The source can be found [on GitHub](https://github.com/getsentry/sentry-java/tree/master/sentry-log4j).

### [Installation](https://docs.sentry.io/platforms/java/guides/log4j2/legacy/log4j.md#installation)

```xml
<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-log4j</artifactId>
    <version>1.7.30</version>
</dependency>
```

For other dependency managers see the [central Maven repository](https://search.maven.org/artifact/io.sentry/sentry-log4j).

### [Usage](https://docs.sentry.io/platforms/java/guides/log4j2/legacy/log4j.md#usage)

The following examples configure 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 `log4j.properties` format:

```ini
# Enable the Console and Sentry appenders
log4j.rootLogger=INFO, Console, Sentry

# Configure the Console appender
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5p: %m%n

# Configure the Sentry appender, overriding the logging threshold to the WARN level
log4j.appender.Sentry=io.sentry.log4j.SentryAppender
log4j.appender.Sentry.threshold=WARN
```

Alternatively, using the `log4j.xml` format:

```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration
  debug="true"
  xmlns:log4j='http://jakarta.apache.org/log4j/'
>

    <!-- Configure the Console appender -->
    <appender name="Console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param
        name="ConversionPattern"
        value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"
      />
        </layout>
    </appender>

    <!-- Configure the Sentry appender, overriding the logging threshold to the WARN level -->
    <appender name="Sentry" class="io.sentry.log4j.SentryAppender">
        <!-- Override the Sentry handler log level to WARN -->
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="levelMin" value="WARN" />
        </filter>
    </appender>

    <!-- 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>
</log4j:configuration>
```

Next, **you’ll need to configure your DSN** (client key) and optionally other values such as `environment` and `release`. [See the configuration page](https://docs.sentry.io/platforms/java/legacy/configuration.md#setting-the-dsn) for ways you can do this.

### [Additional Data](https://docs.sentry.io/platforms/java/guides/log4j2/legacy/log4j.md#additional-data)

It’s possible to add extra data to events thanks to [the MDC](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html) and [the NDC](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html) systems provided by Log4j 1.x.

#### [Mapped Tags](https://docs.sentry.io/platforms/java/guides/log4j2/legacy/log4j.md#mapped-tags)

By default all MDC parameters are stored under the “Additional Data” tab in Sentry. By specifying the `mdctags` 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.

```java
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](https://docs.sentry.io/platforms/java/guides/log4j2/legacy/log4j.md#in-practice)

```java
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.apache.log4j.NDC;

public class MyClass {
  private static final Logger logger = Logger.getLogger(MyClass.class);

  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.
    Sentry.record(
        new BreadcrumbBuilder().setMessage("User made an action").build()
    );

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

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

  void logException() {
    try {
      unsafeMethod();
    } 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!");
  }
}
```

### [Asynchronous Logging](https://docs.sentry.io/platforms/java/guides/log4j2/legacy/log4j.md#asynchronous-logging)

Sentry uses asynchronous communication by default, and so it is unnecessary to use an [AsyncAppender](https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/AsyncAppender.html).
