log4j 2.x
The sentry-log4j2 library provides Log4j 2.x support for Sentry via an Appender 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, in order to do things like record breadcrumbs, set the current user, or manually send events.
The source can be found on GitHub.
Installation
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-log4j2</artifactId>
<version>3.0.0</version>
</dependency>For other dependency managers see the central Maven repository.
Usage
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. 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">
<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).
<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 (see options below). One will configure the lowest level required for a log message to become an event (minimumEventLevel) sent to Sentry. The other option (minimumBreadcrumbLevel) configures the lowest level a message has to be to become a breadcrumb. 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 in order to decide what to keep as breadcrumb or sent as event. Make sure to set the SentryAppender log level configuration to a value lower than what you set for the minimumBreadcrumbLevel and minimumEventLevel to make sure SentryAppender receives these log messages.
<!-- 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"
/>Mapped Tags
By default all ThreadContext parameters are stored under the “Context Data” tab in Sentry.
void logWithExtras() {
// ThreadContext ("MDC") extras
ThreadContext.put("Environment", "Development");
ThreadContext.put("OS", "Linux");
// This sends an event where the Environment and OS ThreadContext values are set as Context Data entries
logger.error("This is a test");
}In Practice
import io.sentry.core.Sentry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
public class MyClass {
private static final Logger logger = LogManager.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.addBreadcrumb("User made an action");
// Log entries below `minimumEventLevel` and above or equal to `minimumBreadcrumbLevel`
// are recorded as breadcrumbs
logger.info("User made another action");
// This sends a simple event to Sentry
logger.error("This is a test");
}
void logWithExtras() {
// MDC extras
ThreadContext.put("extra_key", "extra_value");
// NDC extras are sent under 'log4j2-NDC'
ThreadContext.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!");
}
}