Open renjuen opened 1 year ago
I end up setting the echoHeader
option to true
and getting the request-id from the response header. Works for me.
import Koa from "koa"
import morgan from "koa-morgan"
import rTracer from "cls-rtracer"
const app = new Koa()
app.use(rTracer.koaMiddleware({
echoHeader: true,
})
)
app.use(getMorgan())
app.use(() => {
console.log("it works here", getReqId())
})
app.listen(8080)
const getReqId = () => {
return (rTracer.id() ?? "") as string
}
const getMorgan = () => {
morgan.token("reqId", (req, res) => {
return (res.getHeader("x-request-id") ?? "") as string
})
return morgan("now it works :reqId")
}
Thanks for sharing the workaround! Unfortunately, not all libraries play nicely with ALS.
So I am trying to include the request-id in my logging, but it doesn't work with koa-morgan.
Here's my code:
then when I run the app