Closed guumaster closed 6 years ago
Yes it is, you register the preconfigured logger in a middleware that runs before your controller code. I do this for the same reason, contextual logging. 😄
Here's an example. Include this before your controllers get resolved, and any module that gets resolved in that scope will receive that logger.
app.use((req, res, next) => {
const logger = {
log: (message) => {
console.log(`${req.method} ${req.path}: ${message}`)
}
}
req.container.register({
logger: asValue(logger)
})
next()
})
Thank you for the help, @jeffijoe!
This works perfectly. Thanks!
Is possible to have a scoped logger in a submodule used in the controller?
controller(scoped)
=>moduleA
=>moduleB
.I want to use a
logger
on moduleB scoped to the request (with traceId, correlation, etc). Is it possible? Thanks