---
title: "Logs"
description: "Structured logs allow you to send, view and query logs sent from your applications within Sentry."
url: https://docs.sentry.io/platforms/java/guides/servlet/logs/
---

# Set Up Logs | Sentry for Servlet

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.

## [Requirements](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#requirements)

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

## [Setup](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#setup)

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

```java
import io.sentry.Sentry;

Sentry.init(options -> {
  options.setDsn("___PUBLIC_DSN___");

  options.getLogs().setEnabled(true);

});
```

## [Usage](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#usage)

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.

```java
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:

```java
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"
);
```

### [Scope Attributes](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#scope-attributes)

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.

```java
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.

## [Options](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#options)

#### [beforeSendLog](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#beforesendlog)

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

```java
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.

#### [contextTags](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#contexttags)

You can use the [`contextTags`](https://docs.sentry.io/platforms/java/guides/servlet/configuration/options.md#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.

## [Default Attributes](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#default-attributes)

The Java SDK automatically sets several default attributes on all log entries to provide context and improve debugging:

### [Core Attributes](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#core-attributes)

* `environment`: The environment set in the SDK if defined. This is sent from the SDK as `sentry.environment`.
* `release`: The release set in the SDK if defined. This is sent from the SDK as `sentry.release`.
* `sdk.name`: The name of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.name`.
* `sdk.version`: The version of the SDK that sent the log. This is sent from the SDK as `sentry.sdk.version`.

### [Message Template Attributes](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#message-template-attributes)

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 as `sentry.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 as `sentry.message.parameter.X`.

For example, with the following log:

```java
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 Attributes](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#server-attributes)

* `server.address`: The address of the server that sent the log. Equivalent to `server_name` that gets attached to Sentry errors.

### [User Attributes](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#user-attributes)

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.

### [Integration Attributes](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#integration-attributes)

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 as `sentry.origin`.

### [Scope Attributes](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#scope-attributes)

Any attributes set on the current scope via `Sentry.setAttribute()` or `Sentry.setAttributes()` are automatically included on all log entries. See [Usage](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#usage) above for details.

## [Other Logging Integrations](https://docs.sentry.io/platforms/java/guides/servlet/logs.md#other-logging-integrations)

Available integrations:

* [Spring Boot using Logback](https://docs.sentry.io/platforms/java/guides/spring-boot/logging-frameworks/logback.md)
* [Spring Boot using Log4j2](https://docs.sentry.io/platforms/java/guides/spring-boot/logging-frameworks/log4j2.md)
* [Logback](https://docs.sentry.io/platforms/java/guides/logback/logs.md)
* [Log4j2](https://docs.sentry.io/platforms/java/guides/log4j2/logs.md)
* [JUL](https://docs.sentry.io/platforms/java/guides/jul/logs.md)

If there's an integration you would like to see, open a [new issue on GitHub](https://github.com/getsentry/sentry-java/issues/new/choose).
