Skip to content

Instantly share code, notes, and snippets.

@hbarcelos
Created August 25, 2018 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hbarcelos/44ea8013acefcc36afab34cba1e133c6 to your computer and use it in GitHub Desktop.
Save hbarcelos/44ea8013acefcc36afab34cba1e133c6 to your computer and use it in GitHub Desktop.
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