Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 57658b0 · Browse files · 3 files+66 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+66 added−11 removed

  • other:+66 added−11 removed

3 files+66 added−11 removed

modified:docs/debugging.md+9 added-10 removed
135 unmodified lines
136137// Prepend current process' PID138const execa = execa_({139 verbose(verboseLine) {140 return `[${process.pid}] ${verboseLine}`140 return `[${process.pid}] ${verboseLine}`;141 },142});143```144
19 unmodified lines
164import {execa as execa_} from 'execa';165166const execa = execa_({167 verbose(verboseLine, verboseObject) {168 return JSON.stringify(verboseObject)168 return JSON.stringify(verboseObject);169 },170});171```172
5 unmodified lines
178179// Log to a file using Winston180const transport = new transports.File({filename: 'logs.txt'});181const logger = createLogger({transports: [transport]});182183const execa = execa_({184 verbose(verboseLine, {type, message, ...verboseObject}) {185 const level = LOG_LEVELS[type];186 logger[level](message, verboseObject);187 },188});189182const LOG_LEVELS = {183 command: 'info',184 output: 'verbose',185 ipc: 'verbose',186 error: 'error',187 duration: 'info',188};189190const execa = execa_({191 verbose(verboseLine, {message, ...verboseObject}) {192 const level = LOG_LEVELS[verboseObject.type];193 logger[level](message, verboseObject);194 },195});196```197198<hr>199
3 unmodified lines
modified:types/methods/main-async.d.ts+28 added
316 unmodified lines
317[00:57:44.740] [1] Error: the entrypoint is invalid.318[00:57:44.747] [1] ✘ Command failed with exit code 1: npm run test319[00:57:44.747] [1] ✘ (done in 89ms)320```321322@example <caption>Custom logging</caption>323324```325import {execa as execa_} from 'execa';326import {createLogger, transports} from 'winston';327328// Log to a file using Winston329const transport = new transports.File({filename: 'logs.txt'});330const logger = createLogger({transports: [transport]});331const LOG_LEVELS = {332 command: 'info',333 output: 'verbose',334 ipc: 'verbose',335 error: 'error',336 duration: 'info',337};338339const execa = execa_({340 verbose(verboseLine, {message, ...verboseObject}) {341 const level = LOG_LEVELS[verboseObject.type];342 logger[level](message, verboseObject);343 },344});345346await execa`npm run build`;347await execa`npm run test`;348```349*/350export declare const execa: ExecaMethod<{}>;351352/**
27 unmodified lines
modified:readme.md+29 added-1 removed
56 unmodified lines
57- [Script](#script) interface.58- [No escaping](docs/escaping.md) nor quoting needed. No risk of shell injection.59- Execute [locally installed binaries](#local-binaries) without `npx`.60- Improved [Windows support](docs/windows.md): [shebangs](docs/windows.md#shebang), [`PATHEXT`](https://ss64.com/nt/path.html#pathext), [graceful termination](#graceful-termination), [and more](https://github.com/moxystudio/node-cross-spawn?tab=readme-ov-file#why).61- [Detailed errors](#detailed-error) and [verbose mode](#verbose-mode), for [debugging](docs/debugging.md).61- [Detailed errors](#detailed-error), [verbose mode](#verbose-mode) and [custom logging](#custom-logging), for [debugging](docs/debugging.md).62- [Pipe multiple subprocesses](#pipe-multiple-subprocesses) better than in shells: retrieve [intermediate results](docs/pipe.md#result), use multiple [sources](docs/pipe.md#multiple-sources-1-destination)/[destinations](docs/pipe.md#1-source-multiple-destinations), [unpipe](docs/pipe.md#unpipe).63- [Split](#split-into-text-lines) the output into text lines, or [iterate](#iterate-over-text-lines) progressively over them.64- Strip [unnecessary newlines](docs/lines.md#newlines).65- Pass any [input](docs/input.md) to the subprocess: [files](#file-input), [strings](#simple-input), [`Uint8Array`s](docs/binary.md#binary-input), [iterables](docs/streams.md#iterables-as-input), [objects](docs/transform.md#object-mode) and almost any [other type](#any-input-type).
343 unmodified lines
409```410411<img alt="execa verbose output" src="media/verbose.png" width="603">412413#### Custom logging414415```js416import {execa as execa_} from 'execa';417import {createLogger, transports} from 'winston';418419// Log to a file using Winston420const transport = new transports.File({filename: 'logs.txt'});421const logger = createLogger({transports: [transport]});422const LOG_LEVELS = {423 command: 'info',424 output: 'verbose',425 ipc: 'verbose',426 error: 'error',427 duration: 'info',428};429430const execa = execa_({431 verbose(verboseLine, {message, ...verboseObject}) {432 const level = LOG_LEVELS[verboseObject.type];433 logger[level](message, verboseObject);434 },435});436437await execa`npm run build`;438await execa`npm run test`;439```440441## Related442443- [gulp-execa](https://github.com/ehmicky/gulp-execa) - Gulp plugin for Execa444- [nvexeca](https://github.com/ehmicky/nvexeca) - Run Execa using any Node.js version
5 unmodified lines