# Set Up Logs | Sentry for Android

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/android/logs.md#requirements)

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

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

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

`AndroidManifest.xml`

```XML
<meta-data android:name="io.sentry.logs.enabled" android:value="true" />
```

## [Usage](https://docs.sentry.io/platforms/android/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"
);
```

## [Integrations](https://docs.sentry.io/platforms/android/logs.md#integrations)

### [Timber](https://docs.sentry.io/platforms/android/logs.md#timber)

The Android SDK automatically adds the `SentryTimberIntegration` if the `sentry-android-timber` dependency was found on the classpath. The integration is added with `minLogsLevel` set to `INFO`.

If you want to customize these levels, please have a look at [Timber docs](https://docs.sentry.io/platforms/android/integrations/timber.md#configure).

If you are not using our Gradle plugin, please have a look at [Timber docs](https://docs.sentry.io/platforms/android/integrations/timber.md#install).

This snippet captures an intentional error, so you can test that logs are working as soon as you set it up:

```kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import java.lang.Exception
import timber.log.Timber

class MyActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    try {
      throw Exception("This is a test.")
    } catch (e: Exception) {
      Timber.e(e)
    }
  }
}
```

### [Logcat](https://docs.sentry.io/platforms/android/logs.md#logcat)

The Sentry Android Gradle Plugin automatically captures Logcat logs of level `WARNING` or higher.

If you would like to change the minimum level, please have a look at [Logcat docs](https://docs.sentry.io/platforms/android/integrations/logcat.md#configure).

This snippet captures an intentional error, so you can test that everything is working once you've set it up:

```kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import io.sentry.Sentry
import android.util.Log

class MyActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.w("MyTag", "Warning message.")

    Sentry.captureException(Exception("My Exception"))
  }
}
```

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

#### [beforeSendLog](https://docs.sentry.io/platforms/android/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.SentryLevel;
import io.sentry.android.core.SentryAndroid;
import android.app.Application;

public class MyApplication extends Application {
  public void onCreate() {
    super.onCreate();
    SentryAndroid.init(this, 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.

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

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

### [Core Attributes](https://docs.sentry.io/platforms/android/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/android/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"

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

* `user.id`: The user ID. Maps to id in the User payload, which is set by default by the SDKs.

If user information is available in the current scope, the following attributes are added to the log:

* `user.name`: The username. Maps to username in the User payload.
* `user.email`: The email address. Maps to email in the User payload.

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

If the log was parameterized (like with `Sentry.logger().error("A %s log message", "formatted");`), Sentry adds the message template and parameters as log attributes.

### [Integration Attributes](https://docs.sentry.io/platforms/android/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`.

## [Troubleshooting](https://docs.sentry.io/platforms/android/logs.md#troubleshooting)

### [Missing Logs for Crashes](https://docs.sentry.io/platforms/android/logs.md#missing-logs-for-crashes)

Logs can get lost in certain crash scenarios, if the SDK can not send the logs before the app terminates. We are [currently working on improving](https://github.com/getsentry/sentry-java/issues/4690) this to ensure that all logs are sent, at the latest on the next app restart.
