Created
August 25, 2018 19:44
-
-
Save hbarcelos/44ea8013acefcc36afab34cba1e133c6 to your computer and use it in GitHub Desktop.
Contextual Logger - Added CLS awareness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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