---
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/native/logs/
---

# Set Up Logs | Sentry for Native

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

Logs for Native are supported in Sentry Native SDK version [0.11.1](https://github.com/getsentry/sentry-native/releases/tag/0.11.1) and above.

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

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

```c
sentry_options_t *options = sentry_options_new();
sentry_options_set_enable_logs(options, 1);
// set other options
sentry_init(options);
```

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

Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `sentry_log_X()` APIs.

The API exposes six methods that you can use to log messages at different log levels: `trace`, `debug`, `info`, `warn`, `error`, and `fatal`.

```c
sentry_log_info("A simple log message");
sentry_log_error("A %s log message", "formatted");
```

### [Additional attributes](https://docs.sentry.io/platforms/native/logs.md#additional-attributes)

In case you want to provide additional attributes directly to the logging functions, you must enable the `logs_with_attributes` option. Any `sentry_log_X()` calls will expect a `sentry_value_t` object of attributes as the first `varg` after the log message.

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.

```c
sentry_options_set_logs_with_attributes(options, true);

sentry_value_t attributes = sentry_value_new_object();
sentry_value_t attr = sentry_value_new_attribute(sentry_value_new_string("my_attribute"), NULL);
sentry_value_t attr_2 = sentry_value_new_attribute(sentry_value_new_int64(INT64_MAX), "fermions");
sentry_value_t attr_3 = sentry_value_new_attribute(sentry_value_new_int64(INT64_MIN), "bosons");
sentry_value_set_by_key(attributes, "my.custom.attribute", attr);
sentry_value_set_by_key(attributes, "number.first", attr_2);
sentry_value_set_by_key(attributes, "number.second", attr_3);

sentry_log_debug("logging with %d custom attributes", attributes, 3);

// if the option is enabled, but you want no additional attributes on a log, pass in an empty sentry_value_new_object()
sentry_log_debug("logging with %s custom attributes", sentry_value_new_object(), "no");
```

For now, logs can only take `bool`, `double`, `string` and signed `int` attributes. If you want to send unsigned integer values, you should convert these to strings before creating the attribute.

These additional attributes take precedence over the default attributes, except for `sentry.message.parameter.X` values; if you pass in a string with format specifiers, these are used for the message parameter values.

```c
sentry_value_t param_attributes = sentry_value_new_object();
sentry_value_t param_attr = sentry_value_new_attribute(sentry_value_new_string("custom parameter"), NULL);
sentry_value_t param_attr_2 = sentry_value_new_attribute(sentry_value_new_string("custom-sdk-name"), NULL);
sentry_value_set_by_key(param_attributes, "sentry.message.parameter.0", param_attr);
sentry_value_set_by_key(param_attributes, "sentry.sdk.name", param_attr_2);

// will only have "custom-sdk-name" as an attribute value, but not "custom parameter" which gets overwritten by "and format-string"
sentry_log_fatal("logging with a custom parameter %s attributes", param_attributes, "and format-string");
```

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

#### [before\_send\_log](https://docs.sentry.io/platforms/native/logs.md#before_send_log)

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

```c
static sentry_value_t
before_send_log_callback(sentry_value_t log, void *user_data)
{
    (void)user_data;
    sentry_value_t attribute = sentry_value_new_object();
    sentry_value_set_by_key(
        attribute, "value", sentry_value_new_string("little"));
    sentry_value_set_by_key(
        attribute, "type", sentry_value_new_string("string"));
    sentry_value_set_by_key(sentry_value_get_by_key(log, "attributes"),
        "coffeepot.size", attribute);
    return log;
}

sentry_options_set_before_send_log(options, before_send_log_callback, NULL);
```

The `before_send_log` function receives a log object and optional `user_data`, and should return the log object if you want it to be sent to Sentry, or it should free the log using `sentry_value_decref(log)` and return a `sentry_value_new_null()` if you want to discard it.

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

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

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

```c
sentry_log_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/native/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.
