1

I am facing an issue in nodejs where I have to print the username in the logs in the Metadata. Each request is having its own username and nodejs being async its overriding the username property that i have created for the singleton log.

I have read about continous-local-storage, but I am looking for a simpler solution without requiring any external libraries.

Is it advisable to create a seperate log instance per each request?

P.S.: I am very new to Node js!!

1 Answers1

0

just an update, I figured out how to solve this but had to rely on the library continous-local-storage.

var myses= require('continuation-local-storage').createNamespace('myses');

app.use(function (req, res, next) {
    myses.bindEmitter(req);
    myses.bindEmitter(res);
    myses.run(function () {
      myses.set('someid','somevalue');
      next();
        });
  });

and in some other file, like custom logger,

var thisSession = require('continuation-local-storage').getNamespace('myses');
thisSession .get('someid');