ts-log
Abstract logger TypeScript interface with a dummy logger that does nothing, useful for libraries.
Last updated 3 months ago by kallaspriit .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install ts-log 
SYNC missed versions from official npm registry.

ts-log

CI Downloads Version License StackGrit Score

A zero-dependency TypeScript logger interface. Let library consumers choose their own logger — or stay silent by default.

Installation

npm install ts-log

Usage

Accept a Logger in your library's constructor, defaulting to dummyLogger (which does nothing):

import { Logger, dummyLogger } from "ts-log";

class MyService {
  constructor(private readonly logger: Logger = dummyLogger) {}

  doWork() {
    this.logger.info("work started");
    // ...
    this.logger.debug("details", { foo: "bar" });
  }
}

Consumers can then pass in any compatible logger — or leave the default:

// silent by default
const service = new MyService();

// or use the built-in console
const service = new MyService(console);

// or use pino, bunyan, winston, etc.
import pino from "pino";
const service = new MyService(pino());

Logger Interface

The interface mirrors console — five methods, each accepting a message and optional parameters:

interface Logger {
  trace(message?: any, ...optionalParams: any[]): void;
  debug(message?: any, ...optionalParams: any[]): void;
  info(message?: any, ...optionalParams: any[]): void;
  warn(message?: any, ...optionalParams: any[]): void;
  error(message?: any, ...optionalParams: any[]): void;
}

Any object matching this shape works — no adapters needed. This includes console, pino, bunyan, winston, and most other Node.js loggers.

Custom Logger

You can implement the interface directly for custom behavior:

import fs from "node:fs";
import { Logger } from "ts-log";

class FileLogger implements Logger {
  private readonly fd: number;

  constructor(filename: string) {
    this.fd = fs.openSync(filename, "a");
  }

  trace(message?: any, ...params: any[]) {
    this.write("TRACE", message, params);
  }
  debug(message?: any, ...params: any[]) {
    this.write("DEBUG", message, params);
  }
  info(message?: any, ...params: any[]) {
    this.write("INFO", message, params);
  }
  warn(message?: any, ...params: any[]) {
    this.write("WARN", message, params);
  }
  error(message?: any, ...params: any[]) {
    this.write("ERROR", message, params);
  }

  private write(level: string, message: any, params: any[]) {
    fs.writeSync(this.fd, `${new Date().toISOString()} ${level} ${message} ${JSON.stringify(params)}\n`);
  }
}

v3 Migration

v3 is a tooling modernization — the Logger interface and dummyLogger API are unchanged.

  • Dual CJS/ESM package (via exports field).
  • "type": "module" added to package.json.
  • Minimum Node.js version is 20.
  • Build output moved from build/src/ to dist/.

License

MIT

Current Tags

  • 3.0.3                                ...           latest (3 months ago)

21 Versions

  • 3.0.3                                ...           3 months ago
  • 3.0.2                                ...           7 months ago
  • 3.0.1                                ...           7 months ago
  • 3.0.0                                ...           8 months ago
  • 2.2.7                                ...           2 years ago
  • 2.2.6                                ...           2 years ago
  • 2.2.5                                ...           4 years ago
  • 2.2.4                                ...           5 years ago
  • 2.2.3                                ...           6 years ago
  • 2.2.0                                ...           6 years ago
  • 2.1.4                                ...           8 years ago
  • 2.1.3                                ...           8 years ago
  • 2.1.2                                ...           8 years ago
  • 2.1.1                                ...           8 years ago
  • 2.1.0                                ...           8 years ago
  • 2.0.0                                ...           9 years ago
  • 1.2.1                                ...           9 years ago
  • 1.2.0                                ...           9 years ago
  • 1.1.1                                ...           9 years ago
  • 1.1.0                                ...           9 years ago
  • 1.0.0                                ...           9 years ago
Maintainers (1)
Downloads
Total 41
Today 0
This Week 0
This Month 23
Last Day 0
Last Week 0
Last Month 15
Dependencies (0)
None
Dev Dependencies (10)

© 2010 - cnpmjs.org x YWFE | Home | YWFE