Skip to content
Myr

debugging.md

This file

Path

docs/debugging.md

202 lines · 5.6 KiB · Markdown

Last changed by

Add custom logging section to `readme.md` (#1131)ehmickybefore adoption6c6e861

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–10why, one step back
1<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## Command10

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 11why, one step back
11[`error.command`](api.md#resultcommand) contains the file and [arguments](input.md#command-arguments) that were run. It is intended for logging or debugging.

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 12why, one step back
12

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 13why, one step back
13[`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.

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 14why, one step back
14

Add `parseCommandString()` (#1054)ehmickybefore adoptionb9474c3

line 15why, one step back
15Since 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).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 16–44why, one step back
16
17```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 mode44

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 45why, one step back
45When 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)).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 46–51why, one step back
46
47```js48// build.js49await execa({verbose: 'short'})`npm run build`;50```51

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 52why, one step back
52```

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 53–59why, one step back
53$ node build.js
54[20:36:11.043] [0] $ npm run build55[20:36:11.885] [0] ✔ (done in 842ms)56```5758### Full mode59

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

line 60why, one step back
60When 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)).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 61–62why, one step back
61
62The output is not logged if either:

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 63why, one step back
63- The [`stdout`](api.md#optionsstdout)/[`stderr`](api.md#optionsstderr) option is [`'ignore'`](output.md#ignore-output) or [`'inherit'`](output.md#terminal-output).

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 64why, one step back
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).

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 65why, one step back
65- The [`encoding`](api.md#optionsencoding) option is [binary](binary.md#binary-output).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 66–69why, one step back
66
67```js68// build.js69await execa({verbose: 'full'})`npm run build`;

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 70why, one step back
70await execa({verbose: 'full'})`npm run test`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 71–72why, one step back
71```
72

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 73why, one step back
73```

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 74why, one step back
74$ node build.js

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 75–83why, one step back
75[00:57:44.581] [0] $ npm run build
76[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)

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 84–87why, one step back
84```
8586### Global mode87

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 88why, one step back
88When 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.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 89–98why, one step back
89
90```js91// build.js9293// This is logged by default94await execa`npm run build`;95// This is not logged96await execa({verbose: 'none'})`npm run test`;97```98

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

line 99why, one step back
99```

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 100–101why, one step back
100$ NODE_DEBUG=execa node build.js
101```

Improve `readme.md` (#999)ehmickybefore adoptionaca3db3

lines 102–107why, one step back
102
103### Colors104105When printed to a terminal, the verbose mode uses colors.106107<img alt="execa verbose output" src="../media/verbose.png" width="603">

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 108why, one step back
108

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 109–139why, one step back
109## Custom logging
110111### 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.118119### Filter logs120121```js122import {execa as execa_} from 'execa';123124// Only print log lines showing the subprocess duration125const execa = execa_({126 verbose(verboseLine, {type}) {127 return type === 'duration' ? verboseLine : undefined;128 },129});130```131132### Transform logs133134```js135import {execa as execa_} from 'execa';136137// Prepend current process' PID138const execa = execa_({139 verbose(verboseLine) {

Add custom logging section to `readme.md` (#1131)ehmickybefore adoption6c6e861

line 140why, one step back
140 return `[${process.pid}] ${verboseLine}`;

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 141–167why, one step back
141 },
142});143```144145### Custom log format146147```js148import {execa as execa_} from 'execa';149150// Use a different format for the timestamp151const execa = execa_({152 verbose(verboseLine, {timestamp}) {153 return verboseLine.replace(timestampRegExp, timestamp.toISOString());154 },155});156157// Timestamp at the start of each log line158const timestampRegExp = /\d{2}:\d{2}:\d{2}\.\d{3}/;159```160161### JSON logging162163```js164import {execa as execa_} from 'execa';165166const execa = execa_({167 verbose(verboseLine, verboseObject) {

Add custom logging section to `readme.md` (#1131)ehmickybefore adoption6c6e861

line 168why, one step back
168 return JSON.stringify(verboseObject);

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 169–188why, one step back
169 },
170});171```172173### Advanced logging174175```js176import {execa as execa_} from 'execa';177import {createLogger, transports} from 'winston';178179// Log to a file using Winston180const transport = new transports.File({filename: 'logs.txt'});181const logger = createLogger({transports: [transport]});182const LOG_LEVELS = {183 command: 'info',184 output: 'verbose',185 ipc: 'verbose',186 error: 'error',187 duration: 'info',188};

Add custom logging section to `readme.md` (#1131)ehmickybefore adoption6c6e861

lines 189–195why, one step back
189
190const execa = execa_({191 verbose(verboseLine, {message, ...verboseObject}) {192 const level = LOG_LEVELS[verboseObject.type];193 logger[level](message, verboseObject);194 },195});

Allow `verbose` option to be a function for custom logging (#1130)ehmickybefore adoption8daf348

lines 196–197why, one step back
196```
197

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 198–202why, one step back
198<hr>
199200[**Next**: 📎 Windows](windows.md)\201[**Previous**: 📞 Inter-process communication](ipc.md)\202[**Top**: Table of contents](../readme.md#documentation)