Skip to content

feat(logging): Add the new custom process output class and inject it into the default list renderer options #622

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading