From f065cfc90db7e603d9a7d3c2d56973fb47dee922 Mon Sep 17 00:00:00 2001 From: Vlad Sirenko Date: Mon, 20 Nov 2023 00:26:50 -0800 Subject: [PATCH 1/3] add prefix in logger --- src/logging/default-logger.ts | 17 ++++++++++++++--- src/logging/quiet/run-tracker.ts | 11 +++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) 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; } From 9f7e577836f77eaea9bc9f47a8e4b81f1aa27d02 Mon Sep 17 00:00:00 2001 From: Vlad Sirenko Date: Mon, 20 Nov 2023 00:44:46 -0800 Subject: [PATCH 2/3] add readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1695c692e..0a60babdf 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` | Prefix each stdout and stderr line from the script| ### Glob patterns From a8612cd85bf96acfa3cd5ee494c166f3a709a19f Mon Sep 17 00:00:00 2001 From: Vlad Sirenko Date: Fri, 5 Jan 2024 11:57:15 -0800 Subject: [PATCH 3/3] Update README.md Co-authored-by: Peter Burns --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0a60babdf..166f6a792 100644 --- a/README.md +++ b/README.md @@ -855,7 +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:
  • `quiet` (default): writes a single dynamically updating line summarizing progress. Only passes along stdout and stderr from commands if there's a failure, or if the command is a service. The planned new default, please try it out.
  • `simple` (default): A verbose logger that presents clear information about the work that Wireit is doing.
  • `metrics`: Like `simple`, but also presents a summary table of results once a command is finished.
  • `quiet-ci` (default when env.CI or !stdout.isTTY): like `quiet` but optimized for non-interactive environments, like GitHub Actions runners.
| -| `WIREIT_LOGGER_PREFIX` | Prefix each stdout and stderr line from the script| +| `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