Skip to content

Commit

Permalink
Add custom logging section to readme.md (#1131)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky authored Jun 22, 2024
1 parent 57658b0 commit 6c6e861
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
19 changes: 9 additions & 10 deletions docs/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ import {execa as execa_} from 'execa';
// Prepend current process' PID
const execa = execa_({
verbose(verboseLine) {
return `[${process.pid}] ${verboseLine}`
return `[${process.pid}] ${verboseLine}`;
},
});
```
Expand Down Expand Up @@ -165,7 +165,7 @@ import {execa as execa_} from 'execa';

const execa = execa_({
verbose(verboseLine, verboseObject) {
return JSON.stringify(verboseObject)
return JSON.stringify(verboseObject);
},
});
```
Expand All @@ -179,21 +179,20 @@ import {createLogger, transports} from 'winston';
// Log to a file using Winston
const transport = new transports.File({filename: 'logs.txt'});
const logger = createLogger({transports: [transport]});

const execa = execa_({
verbose(verboseLine, {type, message, ...verboseObject}) {
const level = LOG_LEVELS[type];
logger[level](message, verboseObject);
},
});

const LOG_LEVELS = {
command: 'info',
output: 'verbose',
ipc: 'verbose',
error: 'error',
duration: 'info',
};

const execa = execa_({
verbose(verboseLine, {message, ...verboseObject}) {
const level = LOG_LEVELS[verboseObject.type];
logger[level](message, verboseObject);
},
});
```

<hr>
Expand Down
30 changes: 29 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ One of the maintainers [@ehmicky](https://github.com/ehmicky) is looking for a r
- [No escaping](docs/escaping.md) nor quoting needed. No risk of shell injection.
- Execute [locally installed binaries](#local-binaries) without `npx`.
- 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).
- [Detailed errors](#detailed-error) and [verbose mode](#verbose-mode), for [debugging](docs/debugging.md).
- [Detailed errors](#detailed-error), [verbose mode](#verbose-mode) and [custom logging](#custom-logging), for [debugging](docs/debugging.md).
- [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).
- [Split](#split-into-text-lines) the output into text lines, or [iterate](#iterate-over-text-lines) progressively over them.
- Strip [unnecessary newlines](docs/lines.md#newlines).
Expand Down Expand Up @@ -410,6 +410,34 @@ await execa`npm run test`;

<img alt="execa verbose output" src="media/verbose.png" width="603">

#### Custom logging

```js
import {execa as execa_} from 'execa';
import {createLogger, transports} from 'winston';

// Log to a file using Winston
const transport = new transports.File({filename: 'logs.txt'});
const logger = createLogger({transports: [transport]});
const LOG_LEVELS = {
command: 'info',
output: 'verbose',
ipc: 'verbose',
error: 'error',
duration: 'info',
};

const execa = execa_({
verbose(verboseLine, {message, ...verboseObject}) {
const level = LOG_LEVELS[verboseObject.type];
logger[level](message, verboseObject);
},
});

await execa`npm run build`;
await execa`npm run test`;
```

## Related

- [gulp-execa](https://github.com/ehmicky/gulp-execa) - Gulp plugin for Execa
Expand Down
28 changes: 28 additions & 0 deletions types/methods/main-async.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,34 @@ $ NODE_DEBUG=execa node build.js
[00:57:44.747] [1] ✘ Command failed with exit code 1: npm run test
[00:57:44.747] [1] ✘ (done in 89ms)
```
@example <caption>Custom logging</caption>
```
import {execa as execa_} from 'execa';
import {createLogger, transports} from 'winston';
// Log to a file using Winston
const transport = new transports.File({filename: 'logs.txt'});
const logger = createLogger({transports: [transport]});
const LOG_LEVELS = {
command: 'info',
output: 'verbose',
ipc: 'verbose',
error: 'error',
duration: 'info',
};
const execa = execa_({
verbose(verboseLine, {message, ...verboseObject}) {
const level = LOG_LEVELS[verboseObject.type];
logger[level](message, verboseObject);
},
});
await execa`npm run build`;
await execa`npm run test`;
```
*/
export declare const execa: ExecaMethod<{}>;

Expand Down

0 comments on commit 6c6e861

Please sign in to comment.