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.
Basic scoped instances
Section titled “Basic scoped instances”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 attemptCorrelation IDs across instances
Section titled “Correlation IDs across instances”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 middlewareapp.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 callapp.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.
Instance-level correlation ID
Section titled “Instance-level correlation ID”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 automaticallyworkerLogger.info("Job started", { meta: { jobId: "j-123" } });workerLogger.info("Job complete", { meta: { duration: "142ms" } });Different file directories per instance
Section titled “Different file directories per instance”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 },});Module-level pattern
Section titled “Module-level pattern”Export a logger instance per module — the Node.js module cache ensures it’s a singleton within that module:
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 — usageimport { logger } from "./logger";
export async function chargeCard(amount: number) { logger.info("Charging card", { meta: { amount } });}