From a1693c77b342414ef5824381d6229ade001ad11a Mon Sep 17 00:00:00 2001 From: Lenin Mehedy Date: Thu, 26 Oct 2023 13:09:27 +1100 Subject: [PATCH] feat(cli): add traceId in CLI logs for easier debugging (#449) Signed-off-by: Lenin Mehedy --- fullstack-network-manager/package-lock.json | 13 ++++++ fullstack-network-manager/package.json | 1 + .../src/core/logging.mjs | 44 +++++++++++++------ 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/fullstack-network-manager/package-lock.json b/fullstack-network-manager/package-lock.json index 5f32a46e3..f63a3d17f 100644 --- a/fullstack-network-manager/package-lock.json +++ b/fullstack-network-manager/package-lock.json @@ -13,6 +13,7 @@ "esm": "^3.2.25", "figlet": "^1.6.0", "inquirer": "^9.2.11", + "uuid": "^9.0.1", "winston": "^3.11.0", "yargs": "^17.7.2" }, @@ -4481,6 +4482,18 @@ "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" }, + "node_modules/uuid": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "bin": { + "uuid": "dist/bin/uuid" + } + }, "node_modules/v8-to-istanbul": { "version": "9.1.3", "resolved": "https://registry.npmjs.org/v8-to-istanbul/-/v8-to-istanbul-9.1.3.tgz", diff --git a/fullstack-network-manager/package.json b/fullstack-network-manager/package.json index 0c9732371..d9d653588 100644 --- a/fullstack-network-manager/package.json +++ b/fullstack-network-manager/package.json @@ -24,6 +24,7 @@ "esm": "^3.2.25", "figlet": "^1.6.0", "inquirer": "^9.2.11", + "uuid": "^9.0.1", "winston": "^3.11.0", "yargs": "^17.7.2" }, diff --git a/fullstack-network-manager/src/core/logging.mjs b/fullstack-network-manager/src/core/logging.mjs index c03de29c5..927d2a4e4 100644 --- a/fullstack-network-manager/src/core/logging.mjs +++ b/fullstack-network-manager/src/core/logging.mjs @@ -1,5 +1,6 @@ import * as winston from 'winston' import {constants} from "./constants.mjs"; +import {v4 as uuidv4} from 'uuid'; import * as util from "util"; const customFormat = winston.format.combine( @@ -52,6 +53,9 @@ const Logger = class { * @constructor */ constructor(level) { + let self = this + this.nextTraceId() + this.winsonLogger = winston.createLogger({ level: level, format: winston.format.combine( @@ -72,36 +76,48 @@ const Logger = class { }); } + nextTraceId() { + this.traceId = uuidv4() + } + + prepMeta(meta) { + if (meta === undefined) { + meta = {} + } + + meta.traceId = this.traceId + return meta + } + showUser(msg, ...args) { console.log(util.format(msg, ...args)) } - critical(msg, ...meta) { - this.winsonLogger.crit(msg, ...meta) + critical(msg, ...args) { + this.winsonLogger.crit(msg, ...args, this.prepMeta()) } - error(msg, ...meta) { - this.winsonLogger.error(msg, ...meta) - console.trace() + error(msg, ...args) { + this.winsonLogger.error(msg, ...args, this.prepMeta()) } - warn(msg, ...meta) { - this.winsonLogger.warn(msg, ...meta) + warn(msg, ...args) { + this.winsonLogger.warn(msg, ...args, this.prepMeta()) } - notice(msg, ...meta) { - this.winsonLogger.notice(msg, ...meta) + notice(msg, ...args) { + this.winsonLogger.notice(msg, ...args, this.prepMeta()) } - info(msg, ...meta) { - this.winsonLogger.info(msg, ...meta) + info(msg, ...args) { + this.winsonLogger.info(msg, ...args, this.prepMeta()) } - debug(msg, ...meta) { - this.winsonLogger.debug(msg, ...meta) + debug(msg, ...args) { + this.winsonLogger.debug(msg, ...args, this.prepMeta()) } } -export function NewLogger(level = 'debug') { +export function NewLogger(level = 'debug') { return new Logger(level) }