Skip to content

Commit f52d111

Browse files
committed
Add the new custom process output class and inject it into the default list renderer options
Signed-off-by: instamenta <[email protected]>
1 parent e4932c4 commit f52d111

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

src/core/logging.mjs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,17 @@ const customFormat = winston.format.combine(
4242
})(),
4343

4444
// use custom format TIMESTAMP [LABEL] LEVEL: MESSAGE
45-
winston.format.printf(data => {
46-
return `${data.timestamp}|${data.level}| ${data.message}`
47-
}),
45+
winston.format.printf(data =>
46+
`${data.timestamp}|${data.level}| ${data.message}`
47+
),
4848

4949
// Ignore log messages if they have { private: true }
50-
winston.format((data, opts) => {
51-
if (data.private) {
52-
return false
53-
}
54-
return data
55-
})()
50+
winston.format((data, opts) =>
51+
data.private ? false : data
52+
)()
5653
)
5754

58-
export const Logger = class {
55+
export class Logger {
5956
/**
6057
* Create a new logger
6158
* @param {string} level logging level as supported by winston library:
@@ -101,12 +98,13 @@ export const Logger = class {
10198
this.winstonLogger.setLevel(level)
10299
}
103100

101+
/** @returns {string} */
104102
nextTraceId () {
105103
this.traceId = uuidv4()
106104
}
107105

108106
/**
109-
* @param {Object|undefined} meta
107+
* @param {Object} [meta]
110108
* @returns {Object}
111109
*/
112110
prepMeta (meta) {

src/core/process_output.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright (C) 2024 Hedera Hashgraph, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the ""License"");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an ""AS IS"" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
'use strict'
18+
import { ProcessOutput } from 'listr2'
19+
20+
export class CustomProcessOutput extends ProcessOutput {
21+
/** @param {Logger} logger */
22+
constructor (logger) {
23+
super()
24+
/** @private */
25+
this._logger = logger
26+
}
27+
28+
/**
29+
* @param {Buffer<string|number>} chunk
30+
* @param {string} encoding
31+
* @param {process.stdout.fd|process.stderr.fd|unknown} fd
32+
*/
33+
write (chunk, encoding, fd) {
34+
const message = chunk.toString()
35+
36+
// Capture stdout as debug, stderr as error
37+
if (fd === process.stdout.fd) {
38+
this._logger.debug(`Listr Process stdout: ${message}`)
39+
} else if (fd === process.stderr.fd) {
40+
this._logger.error(`Listr Process stderr: ${message}`)
41+
} else {
42+
this._logger.info(`Listr Process log: ${message}`)
43+
}
44+
}
45+
}

src/index.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ import {
3232
import 'dotenv/config'
3333
import { K8 } from './core/k8.mjs'
3434
import { AccountManager } from './core/account_manager.mjs'
35+
import { ListrLogger } from 'listr2'
36+
import { CustomProcessOutput } from './core/process_output.mjs'
3537

3638
export function main (argv) {
3739
const logger = logging.NewLogger('debug')
40+
constants.LISTR_DEFAULT_RENDERER_OPTION.logger = new ListrLogger({ processOutput: new CustomProcessOutput(logger) })
3841

3942
try {
4043
// prepare dependency manger registry

0 commit comments

Comments
 (0)