2.4 KiB
2.4 KiB
@logger/sdk
Application logger API and transports. Builds on @logger/shared-schema to emit normalized, redacted LogRecords.
Install
npm install @logger/sdk --registry=http://192.168.0.15:4873
Usage
import {createLogger, BatchTransport, HttpTransport} from '@logger/sdk';
const log = createLogger({
app: 'shop',
service: 'orders',
scope: 'orders.refund',
runtime: 'server',
transport: new BatchTransport(new HttpTransport('http://127.0.0.1:4319/ingest'), {maxBatchSize: 25}),
});
log.info('order placed', {orderId: 'o-1'});
log.error('payment timeout', new Error('card declined'), {orderId: 'o-2'});
await log.flush(); // flush pending batches / in-flight HTTP writes
Logger methods
log.debug(message, attributes?)
log.info(message, attributes?)
log.warn(message, attributes?)
log.error(message, error?, attributes?)
log.child(context): Logger // returns a new logger with merged context
await log.flush()
error serializes the error via @logger/shared-schema. Sensitive attribute keys (password, token, secret, apiKey, authorization, cookie, …) are redacted when the record is created.
Strict mode
By default transport failures are swallowed so logging never crashes the app. Set strict: true in the config to make write reject (and flush throw) on transport errors.
Context
createLogger(config) and child(context) merge these fields across the tree: app, service, scope, attributes, runtime, traceId, spanId, sessionId, userId.
Transports
Each transport implements write(record) and an optional flush().
| Transport | Constructor | Behavior |
|---|---|---|
ConsoleTransport |
new ConsoleTransport(consoleLike?) |
Writes timestamp LEVEL scope message + attributes JSON to the console. Default target is the global console. |
MemoryTransport |
new MemoryTransport() |
Collects records in .records (great for tests). |
BatchTransport |
new BatchTransport(target, {maxBatchSize?}) |
Buffers records and flushes to target when the buffer reaches maxBatchSize (default 25) or on flush(). |
HttpTransport |
new HttpTransport(endpoint, {fetch?, headers?}) |
POSTs each record as JSON. Uses global fetch unless overridden. Throws on non-2xx. |
Compose them, e.g. batched HTTP delivery:
new BatchTransport(new HttpTransport('http://127.0.0.1:4319/ingest'), {maxBatchSize: 25});