Lets say I've some REST api server (maybe Express one).
When the life cycle begins (i.e someone GET 'http://foo/bar') there is some data in the Request
object.
So let's say I've got something like this:
const method1 = require('some-module').method1; app.get('/foo/bar', (req, res, next) => { method1(); });
I want a simple way in the some-module.js
to get a winston instance that somehow knows about all relevant data so I don't need every time to pass the request object all over my code.
The trivial solution is to pass the method1
the object and inside do like
method1(req){ winston.info('my message', {requestId: req.id}; }
But this is ugly because I need to change the signature of all my stuff just for logs.
Another option is to make everything a class that extends winston and do something like
app.get('/foo/bar', (req, res, next) => { const foo = new Foo(new winstonWrapper(req)); });
and than foo.info('msg')
will call something like winston.info('msg',{reqId:req.id})
What an elegant way can you suggest to create a winston instance upon request and use it in other modules easily?
via YardenST
No comments:
Post a Comment