observer-js writes diagnostic logs from its internal modules (the Observer, each
Observed* entity, the updaters, etc.). All of that output is routed through a single,
swappable sink so that the host application stays in full control of where logs go,
what level is kept, and how they are formatted. This document explains the model and
gives copy-paste recipes for funneling the logs into pino, winston, plain console,
or nothing at all.
import { format } from 'node:util';
import { setObserverLogger } from '@observertc/observer-js';
// Funnel every observer-js log line into your own logger.
// Argument order: the module name comes FIRST, then a printf-style format string
// and its substitution values.
setObserverLogger({
trace: (module, ...args) => myLogger.trace(`[${module}] ${format(...args)}`),
debug: (module, ...args) => myLogger.debug(`[${module}] ${format(...args)}`),
info: (module, ...args) => myLogger.info(`[${module}] ${format(...args)}`),
warn: (module, ...args) => myLogger.warn(`[${module}] ${format(...args)}`),
error: (module, ...args) => myLogger.error(`[${module}] ${format(...args)}`),
});Watch the argument order. Do not spread the arguments straight into a logger whose first parameter is the message, such as pino — passing the module name there makes it the entire message and silently drops everything after it. Either collapse the arguments yourself (
util.format, above) or pass the module as structured metadata; see pino.consoleis the exception: it accepts a leading non-format argument, so spreading works there.
That's the whole integration surface. The rest of this document is detail.
There are two interfaces and two functions, all exported from the package root:
import {
createLogger, // (moduleName: string) => Logger
setObserverLogger, // (logger: ObserverLogger) => void
type Logger,
type ObserverLogger,
} from '@observertc/observer-js';Logger— what each internal module holds. Five level methods, each variadic:trace,debug,info,warn,error, all(...args: any[]) => void.ObserverLogger— the single sink everyLoggerforwards to. Same five levels, but each receives the originating module name as the first argument:(module: string, ...args: any[]) => void.
Internally, each file does:
const logger = createLogger('ObservedPeerConnection');
// ...
logger.warn('Received sample without callId. %o', sample);createLogger(moduleName) returns a Logger whose every call forwards to the process-wide
ObserverLogger, injecting moduleName. So the line above ultimately calls:
observerLogger.warn('ObservedPeerConnection', 'Received sample without callId. %o', sample);setObserverLogger(...) replaces that process-wide sink. One call reroutes all logging
from every Observer instance in the process.
ObservedCall ─┐
ObservedClient ┤ createLogger('<module>') ──► the single ObserverLogger ──► your logger
Observer ──────┘ (per module) (set via setObserverLogger)
If you never call setObserverLogger, the built-in sink writes to console:
| Level | Default destination |
|---|---|
trace |
dropped (no-op) |
debug |
console.log |
info |
console.info |
warn |
console.warn |
error |
console.error |
…each prefixed with "[LEVEL] <module>". This means the default is fairly verbose
(debug and up go to the console). For anything beyond local experimentation you should
install your own sink with an appropriate level — see below.
Internal log calls use Node/console-style printf placeholders (%s, %o, %d, %j)
followed by the substitution values, and sometimes a trailing object:
logger.warn('Observed Call with id %s already exists; returning the existing instance', callId);
logger.warn('Received sample without clientId %o', sample);Your ObserverLogger receives these as (module, formatString, ...values) — the module
name is a separate leading argument, it is never part of the message.
That distinction matters more than it looks:
consoleaccepts a leading non-format argument, so spreading the arguments after a module prefix prints everything as expected.- Structured loggers that take the message first — pino, bunyan, roarr — do not. Calling
log.warn(module, ...args)makes the module name the message, and the real format string and its values are then consumed as interpolation values for a string that contains no placeholders. The result is a log line with only the module name in it: the actual message is silently dropped.
With those loggers, either pass the module as the merging object
(log.warn({ module }, ...args), see pino) or collapse the
arguments into a single string yourself with util.format
(see generic).
Any time. The sink is read on every log call, not captured at import, so a later
setObserverLogger(...) immediately affects all subsequent logs. There is no ordering
requirement relative to constructing an Observer. Note it is global/process-wide:
the last call wins and applies to all Observer instances.
import { setObserverLogger } from '@observertc/observer-js';
const noop = () => undefined;
setObserverLogger({ trace: noop, debug: noop, info: noop, warn: noop, error: noop });import { setObserverLogger } from '@observertc/observer-js';
setObserverLogger({
trace: () => undefined,
debug: () => undefined,
info: () => undefined,
warn: (module, ...args) => console.warn(`[WARN] ${module}`, ...args),
error: (module, ...args) => console.error(`[ERROR] ${module}`, ...args),
});observer-js does not depend on pino — you bring your own. pino understands printf
placeholders, and passing { module } as the merging object keeps the module name as a
structured field (so you can filter by it later).
import pino from 'pino';
import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
const root = pino({ level: 'warn', name: 'observer-js' });
const adapter: ObserverLogger = {
trace: (module, ...args) => root.trace({ module }, ...args),
debug: (module, ...args) => root.debug({ module }, ...args),
info: (module, ...args) => root.info({ module }, ...args),
warn: (module, ...args) => root.warn({ module }, ...args),
error: (module, ...args) => root.error({ module }, ...args),
};
setObserverLogger(adapter);Level filtering is handled by pino (level: 'warn' above), so the dropped levels cost
almost nothing. If you prefer a dedicated child logger per module:
const children = new Map<string, pino.Logger>();
const child = (module: string) =>
children.get(module) ?? children.set(module, root.child({ module })).get(module)!;
const adapter: ObserverLogger = {
trace: (m, ...a) => child(m).trace(...a),
debug: (m, ...a) => child(m).debug(...a),
info: (m, ...a) => child(m).info(...a),
warn: (m, ...a) => child(m).warn(...a),
error: (m, ...a) => child(m).error(...a),
};
setObserverLogger(adapter);winston's level methods don't do printf substitution the same way, so format the message
with util.format first:
import { format } from 'node:util';
import { createLogger as createWinston, transports, format as wformat } from 'winston';
import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
const w = createWinston({
level: 'warn',
transports: [new transports.Console()],
format: wformat.json(),
});
const adapter: ObserverLogger = {
trace: (module, ...args) => w.silly(format(...args), { module }),
debug: (module, ...args) => w.debug(format(...args), { module }),
info: (module, ...args) => w.info(format(...args), { module }),
warn: (module, ...args) => w.warn(format(...args), { module }),
error: (module, ...args) => w.error(format(...args), { module }),
};
setObserverLogger(adapter);The most portable adapter — works with any logger that takes a single string:
import { format } from 'node:util';
import { setObserverLogger, type ObserverLogger } from '@observertc/observer-js';
const toLine = (module: string, args: unknown[]) => `[${module}] ${format(...args)}`;
const adapter: ObserverLogger = {
trace: (module, ...args) => myLogger.trace(toLine(module, args)),
debug: (module, ...args) => myLogger.debug(toLine(module, args)),
info: (module, ...args) => myLogger.info(toLine(module, args)),
warn: (module, ...args) => myLogger.warn(toLine(module, args)),
error: (module, ...args) => myLogger.error(toLine(module, args)),
};
setObserverLogger(adapter);Because the sink receives the module name, you can route or drop per module:
const NOISY = new Set(['ObservedPeerConnection']);
setObserverLogger({
trace: () => undefined,
debug: (module, ...args) => { if (!NOISY.has(module)) root.debug({ module }, ...args); },
info: (module, ...args) => root.info({ module }, ...args),
warn: (module, ...args) => root.warn({ module }, ...args),
error: (module, ...args) => root.error({ module }, ...args),
});createLogger is exported, so your application code can emit through the same sink and
inherit whatever routing/level you configured with setObserverLogger:
import { createLogger } from '@observertc/observer-js';
const logger = createLogger('my-app:ingest');
logger.info('accepted %d samples in %dms', count, elapsedMs);- Global, not per-instance.
setObserverLoggersets one process-wide sink shared by everyObserver. There is currently no per-Observerlogger override. - Level filtering belongs in your logger.
observer-jsalways calls the sink fordebug/info/warn/error(andtrace, though the default drops it); decide what to keep inside yourObserverLogger(or, with pino/winston, via theirlevel). - No bundled transport.
observer-jsnever importspino,winston, or any logging library — you wire in whichever you already use. - Module name first. The sink signature is
(module, formatString, ...values). Loggers whose first parameter is the message (pino, bunyan, roarr) will keep only the module name if you spread the arguments into them — see Message format. traceis intended for very high-frequency, per-sample diagnostics; keep it dropped in production.
interface Logger {
trace(...args: any[]): void;
debug(...args: any[]): void;
info(...args: any[]): void;
warn(...args: any[]): void;
error(...args: any[]): void;
}
// Note the leading `module` argument — it is not part of the message.
interface ObserverLogger {
trace(module: string, ...args: any[]): void;
debug(module: string, ...args: any[]): void;
info(module: string, ...args: any[]): void;
warn(module: string, ...args: any[]): void;
error(module: string, ...args: any[]): void;
}
function createLogger(moduleName: string): Logger;
function setObserverLogger(logger: ObserverLogger): void;