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
Provenance
- Commit
6c6e861- Tree
e5c9e69- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
57658b0
3 files changed+66 added−11 removed
modified:docs/debugging.md+9 added-10 removed
135 unmodified lines136 137 // Prepend current process' PID138 const execa = execa_({139 verbose(verboseLine) {140- return `[${process.pid}] ${verboseLine}`140+ return `[${process.pid}] ${verboseLine}`;141 },142 });143 ```144 19 unmodified lines164 import {execa as execa_} from 'execa';165 166 const execa = execa_({167 verbose(verboseLine, verboseObject) {168- return JSON.stringify(verboseObject)168+ return JSON.stringify(verboseObject);169 },170 });171 ```172 5 unmodified lines178 179 // Log to a file using Winston180 const transport = new transports.File({filename: 'logs.txt'});181 const logger = createLogger({transports: [transport]});182-183-const execa = execa_({184- verbose(verboseLine, {type, message, ...verboseObject}) {185- const level = LOG_LEVELS[type];186- logger[level](message, verboseObject);187- },188-});189-182 const LOG_LEVELS = {183 command: 'info',184 output: 'verbose',185 ipc: 'verbose',186 error: 'error',187 duration: 'info',188 };189+190+const execa = execa_({191+ verbose(verboseLine, {message, ...verboseObject}) {192+ const level = LOG_LEVELS[verboseObject.type];193+ logger[level](message, verboseObject);194+ },195+});196 ```197 198 <hr>199 3 unmodified linesmodified:types/methods/main-async.d.ts+28 added
316 unmodified lines317 [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 ```321+322+@example <caption>Custom logging</caption>323+324+```325+import {execa as execa_} from 'execa';326+import {createLogger, transports} from 'winston';327+328+// Log to a file using Winston329+const transport = new transports.File({filename: 'logs.txt'});330+const logger = createLogger({transports: [transport]});331+const LOG_LEVELS = {332+ command: 'info',333+ output: 'verbose',334+ ipc: 'verbose',335+ error: 'error',336+ duration: 'info',337+};338+339+const execa = execa_({340+ verbose(verboseLine, {message, ...verboseObject}) {341+ const level = LOG_LEVELS[verboseObject.type];342+ logger[level](message, verboseObject);343+ },344+});345+346+await execa`npm run build`;347+await execa`npm run test`;348+```349 */350 export declare const execa: ExecaMethod<{}>;351 352 /**27 unmodified linesmodified:readme.md+29 added-1 removed
56 unmodified lines57 - [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 lines409 ```410 411 <img alt="execa verbose output" src="media/verbose.png" width="603">412 413+#### Custom logging414+415+```js416+import {execa as execa_} from 'execa';417+import {createLogger, transports} from 'winston';418+419+// Log to a file using Winston420+const transport = new transports.File({filename: 'logs.txt'});421+const logger = createLogger({transports: [transport]});422+const LOG_LEVELS = {423+ command: 'info',424+ output: 'verbose',425+ ipc: 'verbose',426+ error: 'error',427+ duration: 'info',428+};429+430+const execa = execa_({431+ verbose(verboseLine, {message, ...verboseObject}) {432+ const level = LOG_LEVELS[verboseObject.type];433+ logger[level](message, verboseObject);434+ },435+});436+437+await execa`npm run build`;438+await execa`npm run test`;439+```440+441 ## Related442 443 - [gulp-execa](https://github.com/ehmicky/gulp-execa) - Gulp plugin for Execa444 - [nvexeca](https://github.com/ehmicky/nvexeca) - Run Execa using any Node.js version5 unmodified lines