Skip to content

Commit

Permalink
feat(cli): add traceId in CLI logs for easier debugging (#449)
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <[email protected]>
  • Loading branch information
leninmehedy authored Oct 26, 2023
1 parent 78be823 commit a1693c7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
13 changes: 13 additions & 0 deletions fullstack-network-manager/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fullstack-network-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
44 changes: 30 additions & 14 deletions fullstack-network-manager/src/core/logging.mjs
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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)
}

0 comments on commit a1693c7

Please sign in to comment.