diff --git a/src/core/logging.mjs b/src/core/logging.mjs index 393d06445..c0a2c6188 100644 --- a/src/core/logging.mjs +++ b/src/core/logging.mjs @@ -42,20 +42,17 @@ const customFormat = winston.format.combine( })(), // use custom format TIMESTAMP [LABEL] LEVEL: MESSAGE - winston.format.printf(data => { - return `${data.timestamp}|${data.level}| ${data.message}` - }), + winston.format.printf(data => + `${data.timestamp}|${data.level}| ${data.message}` + ), // Ignore log messages if they have { private: true } - winston.format((data, opts) => { - if (data.private) { - return false - } - return data - })() + winston.format((data, opts) => + data.private ? false : data + )() ) -export const Logger = class { +export class Logger { /** * Create a new logger * @param {string} level logging level as supported by winston library: @@ -101,12 +98,13 @@ export const Logger = class { this.winstonLogger.setLevel(level) } + /** @returns {string} */ nextTraceId () { this.traceId = uuidv4() } /** - * @param {Object|undefined} meta + * @param {Object} [meta] * @returns {Object} */ prepMeta (meta) { diff --git a/src/core/process_output.mjs b/src/core/process_output.mjs new file mode 100644 index 000000000..9bbade135 --- /dev/null +++ b/src/core/process_output.mjs @@ -0,0 +1,45 @@ +/** + * Copyright (C) 2024 Hedera Hashgraph, LLC + * + * Licensed under the Apache License, Version 2.0 (the ""License""); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an ""AS IS"" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +'use strict' +import { ProcessOutput } from 'listr2' + +export class CustomProcessOutput extends ProcessOutput { + /** @param {Logger} logger */ + constructor (logger) { + super() + /** @private */ + this._logger = logger + } + + /** + * @param {Buffer} chunk + * @param {string} encoding + * @param {process.stdout.fd|process.stderr.fd|unknown} fd + */ + write (chunk, encoding, fd) { + const message = chunk.toString() + + // Capture stdout as debug, stderr as error + if (fd === process.stdout.fd) { + this._logger.debug(`Listr Process stdout: ${message}`) + } else if (fd === process.stderr.fd) { + this._logger.error(`Listr Process stderr: ${message}`) + } else { + this._logger.info(`Listr Process log: ${message}`) + } + } +} diff --git a/src/index.mjs b/src/index.mjs index e7c1638d5..ef55545e9 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -32,9 +32,12 @@ import { import 'dotenv/config' import { K8 } from './core/k8.mjs' import { AccountManager } from './core/account_manager.mjs' +import { ListrLogger } from 'listr2' +import { CustomProcessOutput } from './core/process_output.mjs' export function main (argv) { const logger = logging.NewLogger('debug') + constants.LISTR_DEFAULT_RENDERER_OPTION.logger = new ListrLogger({ processOutput: new CustomProcessOutput(logger) }) try { // prepare dependency manger registry