tslog: Universal TypeScript Logger

4.10.2 · active · verified Sun Apr 19

tslog is an extensible logging library designed for both Node.js and browser environments, written in TypeScript. Currently stable at version 4.10.2, it maintains an active release cadence with frequent updates and bug fixes, often addressing compatibility and build issues. Key differentiators include its universal compatibility (Node.js, Browsers, Deno, Bun), full TypeScript support with native source map integration for accurate stack traces, and flexible output options (pretty or JSON). It also handles circular structures, supports sub-loggers, allows object and error interpolation, and features masking for sensitive data, making it suitable for a wide range of application logging needs. The library has zero external dependencies, promoting a lightweight footprint.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a `tslog` logger, configure its basic settings like log level and template, and use various logging methods. It also shows how to create a child logger for module-specific logging.

import { Logger, ILogObj } from "tslog";

// Create a new logger instance with optional configuration
const log: Logger<ILogObj> = new Logger({
  name: "myAppLogger",
  minLevel: 3, // info and above
  prettyLogTemplate: "{{logLevelName}} {{name}} " // Customize output template
});

// Log various messages
log.silly("This is a silly message and will not show with minLevel 3.");
log.trace("Tracing code execution.");
log.debug("Debugging a variable: ", { userId: 123, data: "test" });
log.info("Application started successfully.");
log.warn("Deprecated feature in use.");
log.error("An unexpected error occurred.", new Error("File not found"));
log.fatal("Critical system failure!");

// Example of a child logger for a specific module
const userModuleLog = log.getChildLogger({ name: "UserModule" });
userModuleLog.info("User 'admin' logged in.");

view raw JSON →