Set Up Logs

Structured logs allow you to send, view and query logs sent from your applications within Sentry.

With Sentry Structured Logs, you can send text based log information from your applications to Sentry. Once in Sentry, these logs can be viewed alongside relevant errors, searched by text-string, or searched using their individual attributes.

Logs for Java are supported in Sentry Java SDK version 8.12.0 and above.

To enable logging, you need to initialize the SDK with the logs.enabled option set to true.

Copied
import io.sentry.Sentry;

Sentry.init(options -> {
  options.setDsn("https://examplePublicKey@o0.ingest.sentry.io/0");
options.getLogs().setEnabled(true);
});

Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the Sentry.logger() APIs.

The Sentry.logger() namespace exposes six methods that you can use to log messages at different log levels: trace, debug, info, warn, error, and fatal.

For more advanced use cases, use the Sentry.logger().log() methods.

These properties will be sent to Sentry, and can be searched from within the Logs UI, and even added to the Logs views as a dedicated column.

Copied
import io.sentry.Sentry;

Sentry.logger().info("A simple log message");
Sentry.logger().error("A %s log message", "formatted");

We are still working on Logs. You can follow progress on the following GitHub issues or open a new one for any integration you would like to see.

To filter logs, or update them before they are sent to Sentry, you can use the getLogs().beforeSend option.

Copied
import io.sentry.Sentry;

Sentry.init(options -> {
  options.setDsn("https://examplePublicKey@o0.ingest.sentry.io/0");
options.getLogs().setBeforeSend((logEvent) -> { // Modify the event here: logEvent.setBody("new message body"); return logEvent; });
});

The beforeSend function receives a log object, and should return the log object if you want it to be sent to Sentry, or null if you want to discard it.

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").