Skip to content

Commit

Permalink
Add the new custom process output class and inject it into the defaul…
Browse files Browse the repository at this point in the history
…t list renderer options

Signed-off-by: instamenta <instamenta@abv.bg>
  • Loading branch information
instamenta committed Sep 20, 2024
1 parent e4932c4 commit f52d111
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/core/logging.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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) {
Expand Down
45 changes: 45 additions & 0 deletions src/core/process_output.mjs
Original file line number Diff line number Diff line change
@@ -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<string|number>} 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}`)
}
}
}
3 changes: 3 additions & 0 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f52d111

Please sign in to comment.