Logging & Errors
Built-in structured logging with batched, buffered ingestion. Capture errors, stack traces, and structured context across every platform.
| Level | When to Use |
|---|---|
debug | Verbose diagnostic info. Filtered by default (set minLevel to include). |
info | Normal operational events: user actions, state changes. |
warn | Something unexpected but recoverable: slow queries, deprecation notices. |
error | An error occurred but the app can continue: failed API calls, validation errors. |
fatal | Critical failure. The app cannot continue: out of memory, database down. |
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" });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
});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
| Endpoint | Method | Description |
|---|---|---|
/sdk/logs/ingest | POST | Ingest a single log event |
/sdk/logs/ingest/batch | POST | Ingest a batch of log events (preferred) |