Skip to content

Instantly share code, notes, and snippets.

@aigoncharov
Created February 28, 2019 20:56
Show Gist options
  • Save aigoncharov/86e7674af2fcd6d79611a74abeccc3f2 to your computer and use it in GitHub Desktop.
Save aigoncharov/86e7674af2fcd6d79611a74abeccc3f2 to your computer and use it in GitHub Desktop.
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