SDKs
featurely-logger
Structured logging SDK — sends batched log entries to Featurely from any JavaScript or TypeScript application.
featurely-logger
featurely-logger is a structured logging SDK that works in browsers, Node.js, and edge runtimes. Unlike the other Featurely SDKs it requires a logs:write API key permission and is not auto-configured by the CLI.
Installation
npm install featurely-loggerConfiguration options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | — | Required. API key with logs:write permission |
projectId | string | — | Required. Your project ID |
apiUrl | string | https://www.featurely.no | Custom API endpoint |
source | string | "sdk" | Default source tag on every entry (e.g. "server", "worker") |
batchSize | number | 10 | Max entries to accumulate before an automatic flush |
flushInterval | number | 5000 | Milliseconds to wait before flushing a non-full batch |
minLevel | LogLevel | "info" | Minimum level to send — entries below this are dropped client-side |
autoFlushOnExit | boolean | true | Flush pending logs on page unload / process exit |
Log levels
| Level | Numeric order |
|---|---|
trace | 0 — least severe |
debug | 1 |
info | 2 |
warn | 3 |
error | 4 — most severe |
Entries below minLevel are silently dropped without a network request.
Basic usage
import { FeaturelyLogger } from 'featurely-logger';
const logger = new FeaturelyLogger({
apiKey: process.env.FEATURELY_API_KEY!,
projectId: process.env.FEATURELY_PROJECT_ID!,
});
logger.info('User signed in', { category: 'auth', userId: 'u_123' });
logger.warn('Payment retry', { category: 'billing', data: { attempt: 2 } });
logger.error('Stripe webhook failed', { category: 'billing', data: { statusCode: 500 } });Factory function
import { createLogger } from 'featurely-logger';
export const logger = createLogger({
apiKey: process.env.FEATURELY_API_KEY!,
projectId: process.env.FEATURELY_PROJECT_ID!,
source: 'server',
minLevel: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
});Log options
Every log method accepts an optional LogOptions object as the second argument:
logger.error('Payment failed', {
category: 'billing', // Groups related logs in the dashboard
userId: 'u_123', // Filters logs by user in the dashboard
fingerprint: 'stripe-webhook-500', // Counts occurrences instead of duplicate rows
data: { // Arbitrary structured data
statusCode: 500,
retryCount: 3,
},
});| Field | Type | Description |
|---|---|---|
category | string | Group label (e.g. "auth", "payment") |
data | Record<string, unknown> | Arbitrary key/value data |
userId | string | User identifier for filtering |
fingerprint | string | Dedup key — same fingerprint = one row with occurrence count |
Logging at a dynamic level
const level = determineLevel(error); // 'error' | 'warn' | 'info' | ...
logger.log(level, 'Something happened', { category: 'app' });Manual flush
The logger batches entries and flushes them automatically. Call flush() to send immediately (e.g. before a lambda returns):
await logger.flush();Next.js usage
// lib/logger.ts
import { createLogger } from 'featurely-logger';
export const logger = createLogger({
apiKey: process.env.FEATURELY_API_KEY!,
projectId: process.env.NEXT_PUBLIC_FEATURELY_PROJECT_ID!,
source: 'server',
});// app/api/route.ts
import { logger } from '@/lib/logger';
export async function POST(request: Request) {
try {
const data = await request.json();
logger.info('API request received', { category: 'api', data: { path: '/api/route' } });
// ...
} catch (err) {
logger.error('API handler failed', {
category: 'api',
data: { error: String(err) },
});
await logger.flush(); // ensure logs are sent before the edge function exits
return new Response('Internal error', { status: 500 });
}
}Environment support
| Environment | Support |
|---|---|
| Browser | Full — auto-flushes on beforeunload and pagehide |
| Node.js | Full — auto-flushes on beforeExit and SIGTERM |
| Edge / Workers | Manual flush required — call await logger.flush() before returning |
TypeScript exports
FeaturelyLogger, createLogger, FeaturelyLoggerConfig, LogOptions, LogLevel