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 Spring Boot are supported in Sentry Java SDK version 8.15.0
and above.
To enable logging, you have to enable the feature in your Spring configuration file:
sentry.logs.enabled=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
.
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.
import io.sentry.Sentry;
Sentry.logger().info("A simple log message");
Sentry.logger().error("A %s log message", "formatted");
For more advanced use cases, like attaching custom attributes, use the Sentry.logger().log()
methods:
import io.sentry.Sentry;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributes;
import io.sentry.SentryLogLevel;
import io.sentry.logger.SentryLogParameters;
Sentry.logger().log(
SentryLogLevel.FATAL,
SentryLogParameters.create(
SentryAttributes.of(
SentryAttribute.stringAttribute("my.string-attribute", "some-value"),
SentryAttribute.booleanAttribute("my.bool-attribute", true),
SentryAttribute.integerAttribute("my.int-attribute", 42),
SentryAttribute.doubleAttribute("my.double-attribute", 3.12),
SentryAttribute.named("my.attribute", new Point(1, 2))
)
),
"log message %s",
"param1"
);
It is also possible to use Logback and Spring Boot together to have logs going through Logback sent to Sentry. Take a look at the Logging Framework Integrations page.
Available integrations:
We're actively working on adding more integration support for 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 expose a bean implementing the BeforeSendLogCallback
:
import org.springframework.stereotype.Component;
import io.sentry.SentryLogEvent;
import io.sentry.SentryOptions;
@Component
public class CustomBeforeSendLogCallback implements SentryOptions.Logs.BeforeSendLogCallback {
@Override
public SentryLogEvent execute(SentryLogEvent event) {
// Modify or drop the log event here
if (event.getBody().contains("some PII")) {
// Don't send the log event to Sentry
return null;
}
return event;
}
}
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.
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").