Community JavaScript Snippet

ANSI Colored Logger in 50 Lines

chalk is great. chalk plus chalk-template plus log-symbols plus signal-exit is heavy. Here is the dependency-free ANSI logger I drop into every Node script when I want one-line wins without 14 transitive packages.

ANSI Colored Logger in 50 Lines

chalk is great. chalk plus chalk-template plus log-symbols plus signal-exit is heavy. Here is the dependency-free ANSI logger I drop into every Node script when I want one-line wins without 14 transitive packages.

JavaScript
Frontend
3 snippets
logging
input-output
code-template
carlosherrera

By @carlosherrera

May 12, 2026

·

Updated May 18, 2026

1,187 views

34

4.2 (11)

Two design choices are doing all the work. First, the colors are wrapped in a closure that checks isTTY once at startup; if you redirect output to a file the codes vanish, which is what every log analysis tool needs. Second, the logger is a plain object with one method per level rather than a class, which means you can destructure const { info, error } = logger if you want a chalk-style import. The step helper is the piece I use most: it tags the start of a logical phase and returns the wrapped function's value, so timing wrappers compose nicely. I have shipped this in every internal CLI; it covers the 80% case in fewer lines than the import statement for chalk.