# Network Logging (Ktor)

The Luciq KMP library provides automatic network logging for **Ktor HttpClient**: it intercepts requests and responses, injects W3C trace headers for distributed tracing, and sends logs to the native Luciq SDK for bug reports and APMKmp.

### Install the plugin

Add the plugin to your `HttpClient`:

{% code title="Kotlin" %}

```kotlin
import ai.luciq.kmp.interceptors.LuciqKtorPlugin

val client = HttpClient(Engine) {
    install(LuciqKtorPlugin)
}
```

{% endcode %}

All requests made with this client are logged (URL, method, headers, body, timing, status). The plugin also injects a W3C `traceparent` header when the native SDK supports it.

### How it works

1. **Request**: The plugin captures URL, method, headers, and body, and may add a `traceparent` header.
2. **Response**: It captures status, headers, and body, computes duration, and sends a `NetworkData` record to the native SDK via `NetworkLoggerKmp.networkLog`.
3. **Errors**: Failed requests are logged with error domain and message.

Body size may be truncated by the native SDK. Obfuscation and omission callbacks (see [Obfuscation & Omission](/kmp/setup-luciq-for-kmp/network-logging-ktor/obfuscation-and-omission.md)) are applied before sending to the native SDK.

### Manual logging

If you use another HTTP client, build a `NetworkData` instance and call:

{% code title="Kotlin" %}

```kotlin
scope.launch {
    NetworkLoggerKmp.networkLog(networkData)
}
```

{% endcode %}

You can get a W3C header for your request with:

{% code title="Kotlin" %}

```kotlin
val header = NetworkLoggerKmp.getW3CHeaderForRequest(requestHeaders, startTime)
// Attach header to your request
```

{% endcode %}

### Body and auto-masking

{% code title="Kotlin" %}

```kotlin
NetworkLoggerKmp.setNetworkLogBodyEnabled(true)
NetworkLoggerKmp.setNetworkAutoMaskingEnabled(true)
```

{% endcode %}

* [Obfuscation & Omission](/kmp/setup-luciq-for-kmp/network-logging-ktor/obfuscation-and-omission.md) – Remove or mask sensitive data and skip certain requests


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.luciq.ai/kmp/setup-luciq-for-kmp/network-logging-ktor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
