From 426c02512723a42300964f0dee2e252018fd98f3 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 17 Feb 2021 18:22:01 -0500 Subject: [PATCH] run_cmd: Log to Etherpad logs by default --- src/node/utils/run_cmd.js | 11 ++++++++--- src/node/utils/run_npm.js | 15 +++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/node/utils/run_cmd.js b/src/node/utils/run_cmd.js index 4b30af1fe..2af5381a2 100644 --- a/src/node/utils/run_cmd.js +++ b/src/node/utils/run_cmd.js @@ -38,8 +38,9 @@ const logLines = (readable, logLineFn) => { * @param opts Optional options that will be passed to `child_process.spawn()` with two extensions: * - `stdoutLogger`: Callback that is called each time a line of text is written to stdout (utf8 * is assumed). The line (without trailing newline) is passed as the only argument. If null, - * stdout is not logged. If unset, defaults to no-op. Ignored if stdout is not a pipe. - * - `stderrLogger`: Like `stdoutLogger` but for stderr. + * stdout is not logged. If unset, defaults to logging to the Etherpad logs at log level INFO. + * Ignored if stdout is not a pipe. + * - `stderrLogger`: Like `stdoutLogger` but for stderr and log level ERROR. * * @returns A Promise with `stdout`, `stderr`, and `child` properties containing the stdout stream, * stderr stream, and ChildProcess objects, respectively. @@ -47,7 +48,11 @@ const logLines = (readable, logLineFn) => { module.exports = exports = (args, opts = {}) => { logger.debug(`Executing command: ${args.join(' ')}`); - const {stdoutLogger = () => {}, stderrLogger = () => {}} = opts; + const cmdLogger = log4js.getLogger(`runCmd|${args[0]}`); + const { + stdoutLogger = (line) => cmdLogger.info(line), + stderrLogger = (line) => cmdLogger.error(line), + } = opts; // Avoid confusing child_process.spawn() with our extensions. opts = {...opts}; // Make a copy to avoid mutating the caller's copy. delete opts.stdoutLogger; diff --git a/src/node/utils/run_npm.js b/src/node/utils/run_npm.js index 90d8ade90..869e10877 100644 --- a/src/node/utils/run_npm.js +++ b/src/node/utils/run_npm.js @@ -4,17 +4,12 @@ const log4js = require('log4js'); const runCmd = require('./run_cmd'); const logger = log4js.getLogger('runNpm'); -const npmLogger = log4js.getLogger('npm'); - -const stdoutLogger = (line) => npmLogger.info(line); -const stderrLogger = (line) => npmLogger.error(line); /** - * Wrapper around `runCmd()` that logs output to an npm logger by default. + * Wrapper around `runCmd()` to make it easier to run npm. * * @param args Command-line arguments to pass to npm. - * @param opts See the documentation for `runCmd()`. The `stdoutLogger` and `stderrLogger` options - * default to a log4js logger. + * @param opts See the documentation for `runCmd()`. * * @returns A Promise with additional `stdout`, `stderr`, and `child` properties. See the * documentation for `runCmd()`. @@ -22,7 +17,7 @@ const stderrLogger = (line) => npmLogger.error(line); module.exports = exports = (args, opts = {}) => { const cmd = ['npm', ...args]; // MUST return the original Promise returned from runCmd so that the caller can access stdout. - return runCmd(cmd, {stdoutLogger, stderrLogger, ...opts}); + return runCmd(cmd, opts); }; // Log the version of npm at startup. @@ -30,11 +25,11 @@ let loggedVersion = false; (async () => { if (loggedVersion) return; loggedVersion = true; - const p = runCmd(['npm', '--version'], {stdoutLogger: null, stderrLogger}); + const p = runCmd(['npm', '--version'], {stdoutLogger: null}); const chunks = []; await Promise.all([ (async () => { for await (const chunk of p.stdout) chunks.push(chunk); })(), - p, // Await in parallel to avoid unhandled rejection if np rejects during chunk read. + p, // Await in parallel to avoid unhandled rejection if p rejects during chunk read. ]); const version = Buffer.concat(chunks).toString().replace(/\n+$/g, ''); logger.info(`npm --version: ${version}`);