Core Concepts

Logging & Errors

Built-in structured logging with batched, buffered ingestion. Capture errors, stack traces, and structured context across every platform.

Log Levels

LevelWhen to Use
debugVerbose diagnostic info. Filtered by default (set minLevel to include).
infoNormal operational events: user actions, state changes.
warnSomething unexpected but recoverable: slow queries, deprecation notices.
errorAn error occurred but the app can continue: failed API calls, validation errors.
fatalCritical failure. The app cannot continue: out of memory, database down.

Structured Logging

Get a logger instance from the client. It shares the same API credentials and automatically batches log events.

typescript
const logger = client.getLogger();

// Log with structured context
logger.debug("Debugging query", { queryId: "q_1" });
logger.info("User signed in", { userId: "user_123" });
logger.warn("Rate limit approaching", { current: 980, limit: 1000 });
logger.error(new Error("Payment failed"), { orderId: "o_99" });
logger.fatal("System out of memory");

// Set persistent context for all future logs
logger.setContext({ service: "checkout", version: "2.1.0" });

Error Capture

Capture exceptions with full stack traces and structured metadata. The SDK automatically extracts error details.

typescript
try {
  await riskyOperation();
} catch (err) {
  logger.captureError(err as Error, { userId: "user_123" });
}

// Standalone logger with global error capture
import { ToggleAILogger } from "toggleai-sdk";

const logger = new ToggleAILogger({
  clientId: "pk_live_xxx",
  secret: "sk_live_xxx",
  captureGlobalErrors: true, // catches unhandled errors
  minLevel: "warn",          // only send warnings and above
});

Batching & Flushing

Logs are queued in memory and sent in batches. By default, logs flush every 5 seconds or when 50 events are queued — whichever comes first.

typescript
// Manual flush (e.g., before process.exit)
await logger.flush();

// Or use client.close() which flushes automatically
client.close();

Backend Endpoints

EndpointMethodDescription
/sdk/logs/ingestPOSTIngest a single log event
/sdk/logs/ingest/batchPOSTIngest a batch of log events (preferred)