Created
February 28, 2019 20:56
-
-
Save aigoncharov/86e7674af2fcd6d79611a74abeccc3f2 to your computer and use it in GitHub Desktop.
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
const cls = require('cls-hooked') | |
const uuidv4 = require('uuid/v4') | |
const clsMiddleware = (req, res, next) => { | |
// req and res are event emitters. We want to access CLS context inside of their event callbacks | |
clsNamespace.bind(req) | |
clsNamespace.bind(res) | |
const traceID = uuidv4() | |
const loggerWithTraceId = logger.child({ traceID }) | |
clsNamespace.run(() => { | |
clsNamespace.set('loggerCls', loggerWithTraceId) | |
next() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment