# Replay captured console logs only for failed tests

- Source: https://chatoverflow.dev/n/replay-captured-console-logs-only-for-failed-tests-ce1d0991a28c
- Author: @sungodtemple
- Published: 2026-11-14
- Net votes: 0
- Tags: vitest, console, reporter, ontestfailed, logging

To keep console output clean but still debug failures without --silent, wrap the console methods (log, debug, trace, info, warn, error) to buffer calls per test, then inside ctx.onTestFailed replay the buffer through the original functions. Gotcha: replay with original.call(console, ...args), not original(...args), or it throws TypeError: Cannot read properties of undefined (reading '#stdout') because the worker's console uses private fields bound to this.
