Skip to content

Multi-Instance Loggers

Each new Wiz(config) is a fully independent logger — separate level, masked keys, transports, scope, and correlation ID. Instances share no global state.


import { Wiz } from "@gouranga_samrat/log-wiz";
const dbLogger = new Wiz({ scope: "database", level: "debug" });
const httpLogger = new Wiz({ scope: "http", level: "info" });
const authLogger = new Wiz({ scope: "auth", level: "trace" });
dbLogger.debug("Connection pool ready", { meta: { poolSize: 10 } });
// -> █ DBG ... [database] Connection pool ready
httpLogger.info("GET /healthz -> 200", { meta: { latencyMs: 2 } });
// -> █ INF ... [http] GET /healthz -> 200
authLogger.warn("Failed login attempt", { meta: { ip: "1.2.3.4", attempts: 3 } });
// -> █ WRN ... [auth] Failed login attempt

Attach a per-request ID to link related entries across all loggers:

import { Wiz } from "@gouranga_samrat/log-wiz";
const db = new Wiz({ scope: "db" });
const http = new Wiz({ scope: "http" });
// Express middleware
app.use((req, res, next) => {
const requestId = req.headers["x-request-id"] as string ?? crypto.randomUUID();
req.requestId = requestId;
next();
});
// In your route handler — pass correlationId per call
app.post("/users", async (req, res) => {
const cid = req.requestId;
http.info("Incoming request", { correlationId: cid, meta: { path: req.path } });
db.debug("Running INSERT", { correlationId: cid, meta: { table: "users" } });
http.info("Response sent", { correlationId: cid, meta: { status: 201 } });
});

All three entries share the same correlationId, making them trivially filterable in any log aggregator.


For workers, jobs, or connection handlers where every log always shares the same ID:

const workerLogger = new Wiz({
scope: "queue-worker",
correlationId: `worker-${process.pid}`,
level: "info",
});
// Every entry from this instance carries correlationId automatically
workerLogger.info("Job started", { meta: { jobId: "j-123" } });
workerLogger.info("Job complete", { meta: { duration: "142ms" } });

const appLogger = new Wiz({
scope: "app",
file: { dir: "./logs/app", maxFiles: 7 },
});
const accessLogger = new Wiz({
scope: "access",
format: "json",
file: { dir: "./logs/access", maxFiles: 30 },
});

Export a logger instance per module — the Node.js module cache ensures it’s a singleton within that module:

src/services/payment.ts
import { Wiz } from "@gouranga_samrat/log-wiz";
export const logger = new Wiz({
scope: "payment",
level: process.env.NODE_ENV === "production" ? "info" : "debug",
file: { dir: "./logs", maxFiles: 7 },
});
// src/services/payment.ts — usage
import { logger } from "./logger";
export async function chargeCard(amount: number) {
logger.info("Charging card", { meta: { amount } });
}