JSPM

  • Created
  • Published
  • Downloads 49610
  • Score
    100M100P100Q158010F
  • License Apache-2.0

Enhanced Ansi Color Logging and Stringify for Node.js in type script

Package Exports

  • node-ansi-logger

Readme

Node ansi logger and stringify

npm version npm downloads Node.js CI CodeQL codecov tested with Vitest styled with Oxc linted with Oxc TypeScript TypeScript Native ESM


AnsiLogger is a lightweight, customizable color logger for Node.js.

If you like this project and find it useful, please consider giving it a star on GitHub and sponsoring it.

Buy me a coffee

Features

  • Simple and intuitive API for data logging.
  • No dependencies.
  • Customizable colors and apperance.
  • Supports environment variable NO_COLOR=1 (https://no-color.org/).
  • It is also possible to pass a top level logger (like Homebridge or Matter logger) and AnsiLogger will use it for output instead of console.
  • Includes also a fully customizable stringify funtion with colors (it is bigint aware and manage circular reference).
  • Includes a chainable ANSI tagged template API (ansi) for styling terminal strings directly.

Getting Started

Prerequisites

  • Node.js 20-22-24 installed on your machine.

Installation

To get started with AnsiLogger in your package

npm install node-ansi-logger

Usage

Initializing AnsiLogger:

Create an instance of AnsiLogger.

import { AnsiLogger, AnsiLoggerParams, LogLevel } from 'node-ansi-logger';
const log = new AnsiLogger({ logName: '<your name>' }); // Eventually other params in AnsiLoggerParams

To import the stringify functions

import { stringify, payloadStringify, colorStringify, mqttStringify, debugStringify } from 'node-ansi-logger';

Using the logger:

log.debug('Debug message...', ...parameters);
log.info('Info message...', ...parameters);
log.notice('Notice message...', ...parameters);
log.warn('Warning message', ...parameters);
log.error('Error message', ...parameters);
log.fatal('Fatal message', ...parameters);
log(LogLevel.WARN, 'Warning message', ...parameters);

Using the logger with colors inside the message:

log.debug(`Debug message ${YELLOW}with yellow part${db}`, ...);

Using the logger internal timer:

log.startTimer('Time sensitive code started');
log.stopTimer('Time sensitive code finished');

Using the stringify function:

stringify({...})
colorStringify({...})

Using the ansi tagged template API:

Import the ansi root tag or individual named styles:

import { bold, red, green, cyan, bgBlue, warn, error, fatal, hex, rgb, bgHex, bgRgb } from 'node-ansi-logger';

Apply a single style:

console.log(red`Something went wrong`);
console.log(bold`Important message`);

Chain multiple styles:

console.log(bold.red`Critical error`);
console.log(bgBlue.green`Green on blue`);
console.log(bold.italic.underline`Decorated text`);

Use dynamic RGB or hex colors:

console.log(rgb(255, 128, 0)`Orange text`);
console.log(hex('#ff75d1').bold`Pink bold`);
console.log(bgRgb(30, 30, 30).cyan`Cyan on dark background`);
console.log(bgHex('#1a1a2e').white`White on dark blue`);

Nest styles — outer styles are automatically restored after inner resets:

console.log(green`Connected ${bold.red`FAILED`} retrying...`);
console.log(warn`Server ${error.bold`crashed`} restarting`);

Use the 24-step grayscale ramp (gray0 = darkest, gray23 = lightest):

import { gray0, gray8, gray13, gray20, gray23 } from 'node-ansi-logger';

console.log(gray0`Nearly black`);
console.log(gray8`Dark gray`);
console.log(gray13`Mid gray`);
console.log(gray20`Light gray`);
console.log(gray23`Nearly white`);

Log-level color exports:

import { success, debug, info, notice, warn, error, fatal } from 'node-ansi-logger';

console.log(success`Operation completed`);
console.log(debug`Debug details`);
console.log(fatal`Unrecoverable error`);

Mix ansi styles inside AnsiLogger calls:

import { AnsiLogger } from 'node-ansi-logger';
import { bold, red, green, cyan, yellow } from 'node-ansi-logger';

const log = new AnsiLogger({ logName: 'MyApp' });

log.info(`Device ${bold.cyan`Kitchen Light`} connected`);
log.warn(`Retry ${yellow`${3}`} of ${yellow`5`} — response timeout`);
log.error(`Failed to reach ${bold`192.168.1.10`}: ${red`connection refused`}`);
log.debug(`State changed: ${green`on`}${red`off`}`);

Screenshot

Example Image

Repository setup

Note: This repository uses a new toolchain. It replaces the traditional TypeScript / ESLint / Prettier / Jest stack with a faster, lighter setup.

  • No typescript package — replaced by TypeScript Native. The typescript package is kept only as a publish-time dependency while tsgo is still in preview.
  • No ESLint, no Prettier — replaced by the oxc stack: oxlint for linting and oxfmt for formatting.
  • No Jest — replaced by Vitest, which is much faster and natively supports ESM without extra configuration.
  • Far fewer development dependencies — the number of installed packages drops from ~600 to ~75. A clean install is much faster.
  • Much faster linting and formatting — oxlint and oxfmt run in a fraction of the time required by the ESLint / Prettier pipeline.
  • Much faster builds — tsgo compiles the project in a fraction of the time required by the standard tsc build.
  • Editor support — uses the VS Code extensions for tsgo and oxc to get the same experience in the editor.

Contributing

Contributions to AnsiLogger are welcome.

License

This project is licensed under the MIT License - see the LICENSE file for details.