debugging.md
This file
Path
docs/debugging.md
204 lines · 5.9 KiB · Markdown
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
The walk stopped after 200 commits: lines older than 42bea7f point at it.
Fix style inconsistency in API reference (#1010)ehmickybefore adoption42bea7f
lines 1–14why, one step back1<picture>2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# 🐛 Debugging89## Command1011[`error.command`](api.md#resultcommand) contains the file and [arguments](input.md#command-arguments) that were run. It is intended for logging or debugging.1213[`error.escapedCommand`](api.md#resultescapedcommand) is the same, except control characters are escaped. This makes it safe to either print or copy and paste in a terminal, for debugging purposes.14Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3
line 15why, one step back15Since the escaping is fairly basic, neither `error.command` nor `error.escapedCommand` should be executed directly, including using [`execa()`](api.md#execafile-arguments-options) or [`parseCommandString()`](api.md#parsecommandstringcommand).Fix style inconsistency in API reference (#1010)ehmickybefore adoption42bea7f
lines 16–59why, one step back1617```js18import {execa} from 'execa';1920try {21 await execa`npm run build\ntask`;22} catch (error) {23 console.error(error.command); // "npm run build\ntask"24 console.error(error.escapedCommand); // "npm run 'build\\ntask'"25 throw error;26}27```2829## Duration3031```js32try {33 const result = await execa`npm run build`;34 console.log('Command duration:', result.durationMs); // 15035} catch (error) {36 console.error('Command duration:', error.durationMs); // 15037 throw error;38}39```4041## Verbose mode4243### Short mode4445When the [`verbose`](api.md#optionsverbose) option is `'short'`, the [command](#command), [duration](#duration) and [error messages](errors.md#error-message) are printed on [`stderr`](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)).4647```js48// build.js49await execa({verbose: 'short'})`npm run build`;50```5152```53$ node build.js54[20:36:11.043] [0] $ npm run build55[20:36:11.885] [0] ✔ (done in 842ms)56```5758### Full mode59Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 60why, one step back60When the [`verbose`](api.md#optionsverbose) option is `'full'`, the subprocess' [`stdout`, `stderr`](output.md) and [IPC messages](ipc.md) are also logged. They are all printed on [`stderr`](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)).Fix style inconsistency in API reference (#1010)ehmickybefore adoption42bea7f
lines 61–108why, one step back6162The output is not logged if either:63- The [`stdout`](api.md#optionsstdout)/[`stderr`](api.md#optionsstderr) option is [`'ignore'`](output.md#ignore-output) or [`'inherit'`](output.md#terminal-output).64- The `stdout`/`stderr` is redirected to [a stream](streams.md#output), [a file](output.md#file-output), [a file descriptor](output.md#terminal-output), or [another subprocess](pipe.md).65- The [`encoding`](api.md#optionsencoding) option is [binary](binary.md#binary-output).6667```js68// build.js69await execa({verbose: 'full'})`npm run build`;70await execa({verbose: 'full'})`npm run test`;71```7273```74$ node build.js75[00:57:44.581] [0] $ npm run build76[00:57:44.653] [0] Building application...77[00:57:44.653] [0] Done building.78[00:57:44.658] [0] ✔ (done in 78ms)79[00:57:44.658] [1] $ npm run test80[00:57:44.740] [1] Running tests...81[00:57:44.740] [1] Error: the entrypoint is invalid.82[00:57:44.747] [1] ✘ Command failed with exit code 1: npm run test83[00:57:44.747] [1] ✘ (done in 89ms)84```8586### Global mode8788When the `NODE_DEBUG=execa` [environment variable](https://en.wikipedia.org/wiki/Environment_variable) is set, the [`verbose`](api.md#optionsverbose) option defaults to `'full'` for all commands.8990```js91// build.js9293// This is logged by default94await execa`npm run build`;95// This is not logged96await execa({verbose: 'none'})`npm run test`;97```9899```100$ NODE_DEBUG=execa node build.js101```102103### Colors104105When printed to a terminal, the verbose mode uses colors.106107<img alt="execa verbose output" src="../media/verbose.png" width="603">108Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 109–118why, one step back109## Custom logging110111### Verbose function112113The [`verbose`](api.md#optionsverbose) option can be a function to customize logging.114115It is called once per log line. The first argument is the default log line string. The second argument is the same information but as an object instead (documented [here](api.md#verbose-object)).116117If a string is returned, it is printed on `stderr`. If `undefined` is returned, nothing is printed.118TweaksSindre Sorhusbefore adoption698a1b8
line 119why, one step back119If the function throws, the subprocess fails with that error as [`error.cause`](api.md#errorcause). If it threw while logging the output of [`stdout`](api.md#resultstdout), [`stderr`](api.md#resultstderr) or another file descriptor, that file descriptor's output is then not kept, except with [synchronous methods](execution.md#synchronous-execution).Fix `verbose` discarding IPC messages when its function throwsSindre Sorhusbefore adoptionb548d8b
line 120why, one step back120Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 121–141why, one step back121### Filter logs122123```js124import {execa as execa_} from 'execa';125126// Only print log lines showing the subprocess duration127const execa = execa_({128 verbose(verboseLine, {type}) {129 return type === 'duration' ? verboseLine : undefined;130 },131});132```133134### Transform logs135136```js137import {execa as execa_} from 'execa';138139// Prepend current process' PID140const execa = execa_({141 verbose(verboseLine) {Add custom logging section to `readme.md` (#1131)ehmickybefore adoption6c6e861
line 142why, one step back142 return `[${process.pid}] ${verboseLine}`;Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 143–169why, one step back143 },144});145```146147### Custom log format148149```js150import {execa as execa_} from 'execa';151152// Use a different format for the timestamp153const execa = execa_({154 verbose(verboseLine, {timestamp}) {155 return verboseLine.replace(timestampRegExp, timestamp.toISOString());156 },157});158159// Timestamp at the start of each log line160const timestampRegExp = /\d{2}:\d{2}:\d{2}\.\d{3}/;161```162163### JSON logging164165```js166import {execa as execa_} from 'execa';167168const execa = execa_({169 verbose(verboseLine, verboseObject) {Add custom logging section to `readme.md` (#1131)ehmickybefore adoption6c6e861
line 170why, one step back170 return JSON.stringify(verboseObject);Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 171–190why, one step back171 },172});173```174175### Advanced logging176177```js178import {execa as execa_} from 'execa';179import {createLogger, transports} from 'winston';180181// Log to a file using Winston182const transport = new transports.File({filename: 'logs.txt'});183const logger = createLogger({transports: [transport]});184const LOG_LEVELS = {185 command: 'info',186 output: 'verbose',187 ipc: 'verbose',188 error: 'error',189 duration: 'info',190};Add custom logging section to `readme.md` (#1131)ehmickybefore adoption6c6e861
lines 191–197why, one step back191192const execa = execa_({193 verbose(verboseLine, {message, ...verboseObject}) {194 const level = LOG_LEVELS[verboseObject.type];195 logger[level](message, verboseObject);196 },197});Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348
lines 198–199why, one step back198```199Fix style inconsistency in API reference (#1010)ehmickybefore adoption42bea7f
lines 200–204why, one step back200<hr>201202[**Next**: 📎 Windows](windows.md)\203[**Previous**: 📞 Inter-process communication](ipc.md)\204[**Top**: Table of contents](../readme.md#documentation)