Kotlin & Java SDK
Markdown: this page · all of the docs · index
The official Kotlin client, published to Maven Central. It compiles to Java 11 bytecode, so it works from server-side Kotlin and Java as well as from an Android app.
Installation
Add it to the dependencies of your Gradle build.
dependencies { implementation("com.logduck:logduck:1.0.0")}Minimal example
The fewest settings that work — an API key and a source. One client is enough for a process: it holds no per-request state and is safe to share between threads.
import com.logduck.LogDuckClientimport com.logduck.LogDuckEventimport com.logduck.LogDuckOptionsval logduck = LogDuckClient( LogDuckOptions(apiKey = System.getenv("LOGDUCK_API_KEY"), source = "checkout-api"))logduck.send(LogDuckEvent(type = "order.placed"))Complete example
Every option, with its default. Only the first two are required; the rest are shown set to the value you get by omitting them.
import com.logduck.HttpUrlConnectionTransportimport com.logduck.LogDuckClientimport com.logduck.LogDuckEventimport com.logduck.LogDuckOptionsimport com.logduck.StderrLoggerimport java.time.Durationimport java.time.Instantval logduck = LogDuckClient( options = LogDuckOptions( apiKey = System.getenv("LOGDUCK_API_KEY"), // required - your key, starts with `ld_` source = "checkout-api", // required - which app sent this, max 200 chars throwOnError = false, // default false - true throws instead of returning null retryEnabled = true, // default true - retry once on 5xx, network errors and 429 maxRetryDelay = Duration.ofSeconds(10), // default 10s - never block your caller longer than this timeout = Duration.ofSeconds(30), // default 30s - per-request timeout baseUrl = "https://api.logduck.com", // override only for a self-hosted deployment ), transport = HttpUrlConnectionTransport(), // swap the HTTP layer, e.g. for OkHttp logger = StderrLogger, // default StderrLogger)logduck.send( LogDuckEvent( type = "order.placed", // required - max 100 chars subject = "order_9127", // optional - what the event is about, max 500 chars sessionId = "sess_8f21c", // optional - groups related events, max 256 chars message = "Order placed on the Pro plan", // optional - the notification body, max 500 chars time = Instant.now(), // optional - defaults to server receipt time data = mapOf("total" to 4999, "currency" to "NOK"), // optional - any JSON-shaped map emoji = "\uD83D\uDED2", // optional - max 10 chars ))From Java
send is a suspend function. From Java, or anywhere without a coroutine scope, use sendBlocking.
import com.logduck.LogDuckClient;import com.logduck.LogDuckEvent;import com.logduck.LogDuckOptions;LogDuckClient logduck = new LogDuckClient( new LogDuckOptions(System.getenv("LOGDUCK_API_KEY"), "checkout-api"));logduck.sendBlocking(new LogDuckEvent("order.placed"));send returns null and logs a warning when an event does not get through — a logging SDK that throws can take down the code it was only meant to observe. Set throwOnError = true for a LogDuckException instead, which carries status, body and retryAfter. Note that a catch block sees nothing at all until you turn that option on.
sessionId goes out on the wire as sessionid. A CloudEvents extension attribute name must be lowercase alphanumeric; the SDK maps it for you, and you only need this when comparing against the raw HTTP API.
Full reference and changelog: github.com/Log-Duck/logduck-kotlin