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");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"
);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", Point(1, 2))
)
),
"log message %s",
"param1"
)You can set attributes on the scope that will be automatically included in all log entries captured within that scope. This is useful for attaching contextual information like request IDs or user properties that should appear on every log.
import io.sentry.Sentry;
import io.sentry.ScopeType;
import io.sentry.SentryAttribute;
import io.sentry.SentryAttributes;
// Set an attribute on the global scope so it applies to all logs
Sentry.configureScope(ScopeType.GLOBAL, scope -> {
scope.setAttribute("region", "us-east-1");
});
// Set a single attribute with automatic type inference
Sentry.setAttribute("request.id", "abc-123");
// Or use a factory method to set the type explicitly
Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150));
// Set multiple attributes at once
Sentry.setAttributes(SentryAttributes.of(
SentryAttribute.stringAttribute("tenant", "acme-corp"),
SentryAttribute.booleanAttribute("is_admin", true)
));
// All subsequent logs will include these attributes
Sentry.logger().info("Processing request");
// Remove an attribute when it's no longer relevant
Sentry.removeAttribute("request.id");import io.sentry.Sentry
import io.sentry.ScopeType
import io.sentry.SentryAttribute
import io.sentry.SentryAttributes
// Set an attribute on the global scope so it applies to all logs
Sentry.configureScope(ScopeType.GLOBAL) { scope ->
scope.setAttribute("region", "us-east-1")
}
// Set a single attribute with automatic type inference
Sentry.setAttribute("request.id", "abc-123")
// Or use a factory method to set the type explicitly
Sentry.setAttribute(SentryAttribute.integerAttribute("request.duration_ms", 150))
// Set multiple attributes at once
Sentry.setAttributes(SentryAttributes.of(
SentryAttribute.stringAttribute("tenant", "acme-corp"),
SentryAttribute.booleanAttribute("is_admin", true)
))
// All subsequent logs will include these attributes
Sentry.logger().info("Processing request")
// Remove an attribute when it's no longer relevant
Sentry.removeAttribute("request.id")Attribute types are inferred automatically from the value: String maps to string, Boolean to boolean, integer types (Integer, Long, Short, Byte, BigInteger, AtomicInteger, AtomicLong) to integer, floating-point types (Float, Double, BigDecimal) to double, and Collection or array types to array. You can also use typed factory methods like SentryAttribute.stringAttribute() to set the type explicitly.
Attributes passed directly to a log call override scope attributes with the same key.
<PlatformSection supported={["java.spring-boot"]}> 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.