styling-console

Simple, styled console logging for JavaScript.

  • v1.0.1
  • MIT
  • 0 dependencies
  • ESM
$ npm i styling-console
node server.js
logServer listening on :3000
warnCache miss for /api/users
errorDatabase connection refused
infoRetrying in 5s…

Features

Familiar methods

Wraps log, info, warn, error and debug. Everything else on the console is left untouched.

Node.js and browser

ANSI escape codes in the terminal, %c CSS in DevTools. One API for both.

Per-method defaults

configure() sets a default style for each method and merges on repeated calls.

Non-mutating style()

style() returns a new logger with overrides. The original is never changed, and calls chain.

Usage

Use a local logger, or patch the global console.

Local logger

Import the logger without touching the global console.

app.js
import logger from "styling-console/logger";

logger.configure({
  log: { bold: true, color: "green" },
  error: { bold: true, color: "red" },
});

logger.log("Build complete");
logger.style({ italic: true, color: "blue" }).log("One-off line");

Global console

Importing the package patches console. restore() undoes it, install() re-applies it.

app.js
import { restore } from "styling-console";

console.configure({ error: { bold: true, color: "red" } });

console.error("Something went wrong");
console.style({ background: "cyan", color: "black" }).log("Highlighted");

restore(); // back to the original console

Try it

Pick a style and see the output and the code that produces it.

Text
Colour
Background
output
logHello from styling-console
app.js
import logger from "styling-console/logger";

logger.style({ bold: true, color: "cyan" }).log("Hello from styling-console");

Supported styles

Every colour works as a foreground (color) and a background.

bold italic
  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white

Node.js and browser

Node.js

  • Styles use ANSI escape codes.
  • Colours only appear when the stream is a TTY.
  • FORCE_COLOR forces colours on; 0 or false forces them off.
  • NO_COLOR disables colours.

Browser

  • Styles use %c and CSS in DevTools.
  • No separate API or build step needed.
  • Works with any bundler that supports ESM.

Notes

  • Only string arguments are styled; other values pass through unchanged.
  • style() never changes existing configuration.
  • Passing null for a method in configure() resets it.
  • An unknown colour throws a TypeError.
  • Meant for developer-tool output, not HTML or DOM styling.