Contextual Logger - Added CLS awareness
diff --git a/logger.js b/logger.js | |
index f78dff6..dbac73a 100644 | |
--- a/logger.js | |
+++ b/logger.js | |
@@ -1,7 +1,11 @@ | |
const pino = require('pino'); | |
+const { createNamespace } = require('cls-hooked'); | |
function createLogger(opts, destination) { | |
- return pino(opts, destination); | |
+ const baseLogger = pino(opts, destination); | |
+ const cls = createNamespace('@@logger'); | |
+ | |
+ return Object.assign(baseLogger, { cls }); | |
} | |
module.exports = createLogger; |
diff --git a/logger.test.js b/logger.test.js | |
index a841156..7b137e0 100644 | |
--- a/logger.test.js | |
+++ b/logger.test.js | |
@@ -29,3 +29,9 @@ test(`Properly logs message with context object`, async t => { | |
t.deepEqual(entry.dummy, context.dummy); | |
t.deepEqual(entry.msg, message); | |
}); | |
+ | |
+test(`Exposes a CLS namespace`, t => { | |
+ const logger = createLogger(); | |
+ | |
+ t.truthy(logger.cls); | |
+}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment