Skip to content
jshacks.dev
Debugging

Group related logs with console.group

Indent related console output under a label with console.group and console.groupEnd, so interleaved logs from one request stay readable in Node.js 24.

1 min read
Summarize in AI

When a request handler logs ten lines, the output is hard to read. console.group prints a label and indents everything after it until console.groupEnd.

group.mjsJavaScript
console.group("Request");
console.log("start");
console.group("Query");
console.log("SELECT 1");
console.groupEnd();
console.log("done");
console.groupEnd();
Text
Request
  start
  Query
    SELECT 1
  done

Groups nest, and each level adds two spaces in Node.js. In a browser, the group is collapsible, and console.groupCollapsed starts it closed, which keeps noisy detail out of the way until you open it. The methods are in the Node.js console docs.

Always match each group with a groupEnd. If code throws between them, the indentation stays for every later log, so close the group in a finally block when the code can fail.

Groups only indent text. They do not tie logs to a request across async calls, so use a request id for that.

Share

More snippets

Debugging

Inspect an object with console.dir

Show nested objects beyond the default depth of two with console.dir and the depth option, instead of the [Object] placeholders console.log prints.

1 min read
Debugging

Debug a single Vitest test in VS Code

Run one Vitest file under the VS Code debugger with a launch configuration that passes the open file, so breakpoints in tests and source work. Vitest 3.

1 min read
Debugging

Print a stack trace with console.trace

Find out who called a function with console.trace, which prints the message and the current call stack to stderr. Node.js 24 and browsers.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.