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 JUL are supported in Sentry Java SDK version 8.16.0 and above.
To enable logging, you need to configure the option in sentry.properties or when calling Sentry.init.
logs.enabled=true
You may also configure the Sentry handler and set minimumLevel in logging.properties:
# Configure the Sentry handler
handlers=io.sentry.jul.SentryHandler
# Set the minimum level for Sentry handler
io.sentry.jul.SentryHandler.minimumLevel=CONFIG
Once the handler is configured with logging enabled, any logs at or above the minimumLevel will be sent to Sentry.
To filter logs, or update them before they are sent to Sentry, you can use the getLogs().beforeSend option.
import io.sentry.Sentry;
Sentry.init(options -> {
options.setDsn("___PUBLIC_DSN___");
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.
You can use the contextTags option to include specific properties from the Mapped Diagnostic Context (MDC) as attributes on log entries sent to Sentry.
For detailed configuration examples, see the Advanced Usage documentation for your logging framework.
The Java SDK automatically sets several default attributes on all log entries to provide context and improve debugging:
environment: The environment set in the SDK if defined. This is sent from the SDK assentry.environment.release: The release set in the SDK if defined. This is sent from the SDK assentry.release.sdk.name: The name of the SDK that sent the log. This is sent from the SDK assentry.sdk.name.sdk.version: The version of the SDK that sent the log. This is sent from the SDK assentry.sdk.version.
If the log was parameterized, Sentry adds the message template and parameters as log attributes.
message.template: The parameterized template string. This is sent from the SDK assentry.message.template.message.parameter.X: The parameters to fill the template string. X can either be the number that represent the parameter's position in the template string (sentry.message.parameter.0,sentry.message.parameter.1, etc) or the parameter's name (sentry.message.parameter.item_id,sentry.message.parameter.user_id, etc). This is sent from the SDK assentry.message.parameter.X.
For example, with the following log:
Sentry.logger().error("A %s log message", "formatted");
Sentry will add the following attributes:
message.template: "A %s log message"message.parameter.0: "formatted"
server.address: The address of the server that sent the log. Equivalent toserver_namethat gets attached to Sentry errors.
If user information is available in the current scope, the following attributes are added to the log:
user.id: The user ID.user.name: The username.user.email: The email address.
If a log is generated by an SDK integration, the SDK will set additional attributes to help you identify the source of the log.
origin: The origin of the log. This is sent from the SDK assentry.origin.
Any attributes set on the current scope via Sentry.setAttribute() or Sentry.setAttributes() are automatically included on all log entries. See Usage above for details.
Available integrations:
If there's an integration you would like to see, open a new issue on GitHub.
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").