diff --git a/README.md b/README.md index 1695c692e..166f6a792 100644 --- a/README.md +++ b/README.md @@ -855,6 +855,7 @@ The following environment variables affect the behavior of Wireit: | `CI` | Affects the default value of `WIREIT_CACHE`.

Automatically set to `true` by [GitHub Actions](https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables) and most other CI (continuous integration) services.

Must be exactly `true`. If unset or any other value, interpreted as `false`. | | `WIREIT_MAX_OPEN_FILES` | Limits the number of file descriptors Wireit will have open concurrently. Prevents resource exhaustion when checking large numbers of cached files. Set to a lower number if you hit file descriptor limits. | | `WIREIT_LOGGER` | How to present progress and results on the command line.

Options:
| +| `WIREIT_LOGGER_PREFIX` | When this env variable is present and not empty, Wireit will prefix each stdout and stderr line from the script with its name. | ### Glob patterns diff --git a/src/logging/default-logger.ts b/src/logging/default-logger.ts index 0b636f260..8455c4473 100644 --- a/src/logging/default-logger.ts +++ b/src/logging/default-logger.ts @@ -7,7 +7,7 @@ import * as pathlib from 'path'; import {unreachable} from '../util/unreachable.js'; -import type {Event} from '../event.js'; +import type {Event, Stderr, Stdout} from '../event.js'; import type {Logger, Console} from './logger.js'; import {type PackageReference, type ScriptReference} from '../config.js'; import {DiagnosticPrinter} from '../error.js'; @@ -42,6 +42,8 @@ export class DefaultLogger implements Logger { readonly console: Console; readonly #diagnosticPrinter: DiagnosticPrinter; + private outputWrite: typeof DefaultLogger.prototype.outputWriteDebug; + /** * @param rootPackage The npm package directory that the root script being * executed belongs to. @@ -50,6 +52,15 @@ export class DefaultLogger implements Logger { this.#rootPackageDir = rootPackage; this.#diagnosticPrinter = new DiagnosticPrinter(this.#rootPackageDir); this.console = ourConsole; + this.outputWrite = process.env['WIREIT_LOGGER_PREFIX'] ? this.outputWriteDebug : this.outputWriteDefault; + } + + private outputWriteDefault(this: void, stream: NodeJS.WritableStream, event: Stdout | Stderr) { + stream.write(event.data); + } + + private outputWriteDebug(this: void, stream: NodeJS.WritableStream, event: Stdout | Stderr, label: string) { + stream.write(event.data.toString().split('\n').map(line => line.trim() ? `[${label}] ${line}` : line).join('\n')); } log(event: Event) { @@ -222,11 +233,11 @@ export class DefaultLogger implements Logger { // TODO(aomarks) More advanced handling of output streams so that // output isn't simply interweaved. case 'stdout': { - this.console.stdout.write(event.data); + this.outputWrite(this.console.stdout, event, label); break; } case 'stderr': { - this.console.stderr.write(event.data); + this.outputWrite(this.console.stderr, event, label); break; } } diff --git a/src/logging/quiet/run-tracker.ts b/src/logging/quiet/run-tracker.ts index 94f4fcfa7..a370610f7 100644 --- a/src/logging/quiet/run-tracker.ts +++ b/src/logging/quiet/run-tracker.ts @@ -600,6 +600,13 @@ export class QuietRunLogger implements Disposable { return state.hasBufferedOutput; } + private outputWrite = process.env['WIREIT_LOGGER_PREFIX'] ? (stream: NodeJS.WritableStream, output: Output) => { + const label = labelForScript(this.#rootPackage, output.script); + stream.write(output.data.toString().split('\n').map(line => line.trim() ? `[${label}] ${line}` : line).join('\n')); + } : (stream: NodeJS.WritableStream, output: Output) => { + stream.write(output.data); + } + #handleOutput(event: Output): StatusLineResult { const key = scriptReferenceToString(event.script); const state = this.#running.get(key); @@ -618,9 +625,9 @@ export class QuietRunLogger implements Disposable { // eslint-disable-next-line @typescript-eslint/no-unused-vars using _pause = this.#statusLineWriter.clearUntilDisposed(); if (event.stream === 'stdout') { - process.stdout.write(event.data); + this.outputWrite(process.stdout, event); } else { - process.stderr.write(event.data); + this.outputWrite(process.stderr, event); } return noChange; }