Advanced Usage
It’s possible to add extra data to events thanks to the MDC system provided by Logback.
Mapped Tags
By default all MDC parameters are stored under the “MDC” tab in Sentry.
Copied
import org.slf4j.MDC;
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 MDC entries
logger.error("This is a test");
}
Note that MDC manages data on a per-thread basis, and that a child thread does not automatically inherit MDC properties from its parent.
In Practice
Copied
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
public class MyClass {
private static final Logger logger = LoggerFactory.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
MDC.put("extra_key", "extra_value");
// 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!");
}
}
- Package:
- maven:io.sentry:sentry-logback
- Version:
- 7.13.0
- Repository:
- https://github.com/getsentry/sentry-java