diff --git a/packages/module-logger/readme.md b/packages/module-logger/readme.md index e6edfc0..7e265b2 100644 --- a/packages/module-logger/readme.md +++ b/packages/module-logger/readme.md @@ -84,6 +84,7 @@ logger.info("This is a modularized info log written to a file"); }, }, transportOverrides: [], // array of transport overrides - globalAttributes: {}, // attributes which will be added to all logs + globalAttributes: {}, // attributes which will be added to all logs, + traceKey: "correlationId", // key to be used for tracing } ``` diff --git a/packages/module-logger/src/index.js b/packages/module-logger/src/index.js index 63ca031..a33316f 100644 --- a/packages/module-logger/src/index.js +++ b/packages/module-logger/src/index.js @@ -16,14 +16,15 @@ let _defaultConfig = { enabled: false, options: {} }, - globalAttributes: {} + globalAttributes: {}, + traceKey: "correlationId" }; -const _defaultKeys = ["level", "correlationId", "timestamp", "message"]; - const _createLogger = () => { let transports = []; + const _defaultKeys = ["level", _defaultConfig.traceKey, "timestamp", "message"]; + if (_defaultConfig.console?.enabled) { transports.push( new winston.transports.Console({ @@ -54,12 +55,12 @@ const _createLogger = () => { _logger = winston.createLogger({ format: winston.format.combine( winston.format((infoObj) => { - let correlationId = context.get("correlationId"); + let correlationId = context.get(_defaultConfig.traceKey); if (!correlationId) { correlationId = crypto.randomBytes(16).toString("hex"); - context.set("correlationId", correlationId); + context.set(_defaultConfig.traceKey, correlationId); } - infoObj["correlationId"] = chalk.blue(correlationId); + infoObj[_defaultConfig.traceKey] = chalk.blue(correlationId); return { ...infoObj, ...(_defaultConfig.globalAttributes ?? {}) }; })(), winston.format.timestamp(),