# Logging

{% hint style="warning" %}
Logging is disabled by default.
{% endhint %}

{% hint style="success" %}
Please note logs **do not** include any Personally Identifiable Information (PII).
{% endhint %}

#### How it works

Firstly, there are two options available related to logging data:

1. If you want to enable logging to the Keyless infrastructure use `keylessLogsConfiguration` . This options ensures Keyless has access to richer SDK logs to support investigations on our side, and also enriches the [Keyless dashboard](https://dash.keyless.io/).
2. Alternatively, if you wish to collect logging for you own analytics without sending them to the Keyless infrastructure use `customLogsConfiguration`

{% tabs %}
{% tab title="Android" %}

```kotlin
val setup = SetupConfig(
    apiKey = "...",
    hosts = listOf(""),
    keylessLogsConfiguration = LogsConfiguration(
        enabled = true,
        logLevel = LogLevels.INFO
    ),
    customLogsConfiguration = LogsConfiguration(
        enabled = true
    )
)
```

{% endtab %}

{% tab title="iOS" %}

```swift
let configuration = SetupConfig(
    apiKey: "some api key",
    hosts: ["some.host"],
    keylessLogsConfiguration: KeylessLogsConfiguration(enabled: true),
    customLogsConfiguration: CustomLogsConfiguration(enabled: true, logLevel: .INFO, callback: { event in
        print(event)
    })
)

Keyless.configure(setupConfiguration: configuration) { error in
    // handle error
}
```

{% endtab %}

{% tab title="Android 4.6" %}

```kotlin
val setupConfiguration = SetupConfiguration.builder
    .withApiKey("")
    .withHosts(listOf("..."))
    .withLogging(
        keylessLogsConfiguration = LogsConfiguration(enabled = true),
        customLogsConfiguration = LogsConfiguration(enabled = true, logLevel = LogLevels.INFO)
    )
    .build()
```

{% endtab %}

{% tab title="iOS 4.6" %}

```kotlin
let configuration = Keyless.SetupConfiguration
  .builder
  .withApiKey("some api key")
  .withHosts(["some.host"])
  .withLogging(
    keylessLogsConfiguration: KeylessLogsConfiguration(enabled: true, logLevel: .INFO),
    customLogsConfiguration: CustomLogsConfiguration(enabled: true, callback: { event in
      print(event)
    })
  )
  .build()
 
Keyless.configure(setupConfiguration: configuration) { error in
  // handle error
}
```

{% endtab %}

{% tab title="Flutter" %}

```dart
final configuration = SetupConfiguration(
              apiKey: apiKey,
              hosts: [host],
              loggingEnabled: true,
              loggingLevel: LogLevel.info));

try {
  final result = await Keyless.instance.configure(configuration);
  print("Configure finished succcessfully.");
} catch (error) {
  print("Configure finished with error: $error");
}
```

{% endtab %}

{% tab title="React Native" %}

```javascript
    const config = new SetupConfig({
      apiKey: 'apiKey',
      hosts: ['HOSTS'],
      keylessLogsConfiguration: new LogsConfiguration({
        enabled: true,
        logLevel: LogLevels.INFO,
      }),
      customLogsConfiguration: new LogsConfiguration({
        enabled: true,
        logLevel: LogLevels.TRACE,
      }),
    });
    const result = await Keyless.configure(config);

    result.fold({
      onSuccess: _data => {
        //Handle success
      },
      onFailure: _error => {
        //Handle error
      },

```

{% endtab %}
{% endtabs %}

Collect logs after setting up custom logging with `customLogsConfiguration` option:

{% tabs %}
{% tab title="Android" %}

1. Start collecting the `Keyless.customLogs` flow (This must happen before a `Keyless.configure()` to get all Logs Events):

```kotlin
Keyless.customLogs.collect { logEvent ->
    // handle the logEvent
}
```

2. Configure the SDK with the following `SetupConfig`:

```kotlin
val setup = SetupConfig(
    apiKey = "...",
    hosts = listOf(""),
    customLogsConfiguration = LogsConfiguration(
        enabled = true,
        logLevel = LogLevels.INFO // This is optional and defaults to INFO
    )
)
```

{% endtab %}

{% tab title="iOS" %}

```swift
var myEventCollection = [LogEvent]()
    let configuration = Keyless.SetupConfiguration
      .builder
      .withApiKey("some api key")
      .withHosts(["some.host"])
      .withLogging(
        keylessLogsConfiguration: KeylessLogsConfiguration(enabled: true, logLevel: .INFO),
        customLogsConfiguration: CustomLogsConfiguration(enabled: true, callback: { event in
          myEventCollection.append(event)
        })
      )
     .build()
    Keyless.configure(setupConfiguration: configuration) { error in
      // handle error
    }
}
```

{% endtab %}

{% tab title="React Native" %}

1. Start collecting Keyless suctom logs flow (This must happen before a `Keyless.configure()` to get all Logs Events)

```javascript
    const eventSubscription = Keyless.subscribeToCustomLogs(eventLog => {
        // Handle log
    });
    const config = new SetupConfig({
      apiKey: 'apiKey',
      hosts: ['HOSTS'],
      customLogsConfiguration: new LogsConfiguration({
        enabled: true,
        logLevel: LogLevels.TRACE,
      }),
    });
    const result = await Keyless.configure(config);

    result.fold({
      onSuccess: _data => {
        //Handle success
      },
      onFailure: _error => {
        //Handle error
      },

```

2. When you want to unsubscribe from logs make sure you call:

```javascript
    eventSubscription?.unsubscribe();
```

{% endtab %}
{% endtabs %}

#### Logging Levels

You may set different logging levels to provide more or less information in logs.

The available levels are the following:

* `INFO` (default)
* `DEBUG`
* `TRACE`

The `TRACE` level provides the following additional data:

* `userId`
* `devicePublicSigningKey`
* `coreLogHistory` (used for detailed debugging)
