Chalk Structured Output
Color CLI output with Chalk to signal severity, keep color optional with a no-color flag, and structure lines so they parse as well as they read.
On this page
Introduction
This guide covers structured CLI output with Chalk, a recurring concern in the cli-tooling scenario. Color draws the eye to errors and highlights the important field, but a CLI that only works in color is useless in a log file and hostile to colorblind users. Chalk with a no-color flag, a severity palette, and parseable lines gives the benefit without the trap.
Palette by severity
Use red for errors, yellow for warnings, and green for success, so color carries meaning rather than decoration, and colorblind users get the same signal from the word.
The trade-off worth naming is that use red for errors, yellow for warnings, and green for success, so color carries meaning rather than decoration, and colorblind users get the same signal from the word adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
import chalk from 'chalk';
const c = { error: chalk.red, warn: chalk.yellow, ok: chalk.green };
function log(level, msg) { console.log(c[level](level.toUpperCase()) + ': ' + msg); }
Respect no-color
Check a –no-color flag and the NO_COLOR env var and set the Chalk level to 0 when disabled, so output is plain in logs and pipes.
The trade-off worth naming is that check a –no-color flag and the no_color env var and set the chalk level to 0 when disabled, so output is plain in logs and pipes adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
if (opts.noColor || process.env.NO_COLOR) chalk.level = 0;
Parseable lines
Keep a consistent format like LEVEL: message so a grep or awk can filter by severity, which makes the output a tool as well as a display.
The trade-off worth naming is that keep a consistent format like level: message so a grep or awk can filter by severity, which makes the output a tool as well as a display adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Color the label only
Color the severity word, not the whole line, so the value stays readable and copy-pasteable and a log does not carry escape codes through the data.
The trade-off worth naming is that color the severity word, not the whole line, so the value stays readable and copy-pasteable and a log does not carry escape codes through the data adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Symbols over color
Pair color with a symbol like a check or a cross so the meaning survives without color, which helps colorblind users and plain-text logs.
The trade-off worth naming is that pair color with a symbol like a check or a cross so the meaning survives without color, which helps colorblind users and plain-text logs adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test plain output
Run with NO_COLOR set and assert the output has no escape codes and is grep-friendly, so the no-color path is real.
The trade-off worth naming is that run with no_color set and assert the output has no escape codes and is grep-friendly, so the no-color path is real adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: Chalk, NO_COLOR spec.
FAQ
- Should I color the whole line or just the label?
- Just the label or the severity word, so the value stays readable and copy-pasteable; coloring the whole line makes the data hard to extract in a log.
- How do I disable color?
- Check a --no-color flag and the NO_COLOR env var, and pass a level to Chalk so it strips codes when disabled, which is the standard convention.
Report a problem with this articleOpens a pre-filled issue on GitHub.