rojanu
rojanu

Reputation: 1642

Logging with traceId in Express.js

I need to produce logs with a trace id. Currently we are using winston to do the logging

I am using winston containers as follows

var fs = require('fs');
var os = require('os');
var path = require('path');
var winston = require('winston');
var transports = [];

module.exports = function(setting) {

  function formatter(options) {
    return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '');
  }

  if (setting.log.file) {
    fs.existsSync(setting.log.path) || fs.mkdirSync(setting.log.path);
    transports.push(new winston.transports.DailyRotateFile({
      handleExceptions: true,
      json: true,
      component: setting.customise.app_type,
      datePattern: setting.log.datePattern,
      filename: path.join(setting.log.path, setting.log.filename),
      level: setting.log.level
    }));
  }
  if (setting.log.console || transports.length === 0) {
    transports.push(new (winston.transports.Console)({
      timestamp: function() {
        return new Date().toISOString();
      },
      handleExceptions: true,
      json: false,
      formatter: formatter,
      level: setting.log.level,
      colorize: 'all'
    }));
  }

  winston.loggers.add('defaultLogger', {
    transports: transports
  });

  var logger = winston.loggers.get('defaultLogger');
  logger.exitOnError = false;
  logger.addFilter(function(msg, meta, level) {
    if(!meta){
      meta = {};
    }
    meta.component= setting.customise.app_type;
    meta.component_version = setting.app_version;
    meta.machine = os.hostname();
    meta.context = process.pid;
    return msg;
  });
};

So in the files that need to do some logging, simply do

var sessionTool = require('sessionTool')(app);

var logger = require('winston').loggers.get('defaultLogger');

to get express to use winston

var winstonStream = {
      write: function(message, encoding){
          logger.info(message.slice(0, -1));
      }
  };
  app.use(express.logger({
      stream: winstonStream,
      format: ':remote-addr - - :method :url HTTP/:http-version :status :res[content-length] :referrer :user-agent'
  }));

I added a capture all route to get/generate a trace id

app.all('*', function(req, res, next){
  var traceId = req.params['trace_id'];
  if(!traceId){
    traceId = require('node-uuid').v4();
  }
  req.params['trace_id'] = traceId;
});

So the question is: How can the unique trace id for each call made available thorough the application?

UPDATE: To make sure that the trace id is in used at all times, I am thinking of create an global variable for every request without passing the variable from request down to controllers and then down again, as that would mean that everytime a new controller or a service is introduced then we have to remember the trace id as well.

Upvotes: 2

Views: 8931

Answers (1)

rojanu
rojanu

Reputation: 1642

I have ended using continuation-local-storage to store trace_id and use it later when logging

Add the following to winston filter

logger.addFilter(function(msg, meta, level) {
   var nameSpace = require('continuation-local-storage').getNamespace('nameSpace');
   ....
   meta.trace_id = meta.trace_id || nameSpace.get('trace_id');
   return msg;
}

and change app.all() function to

server.all('*', function(req, res, next) {
  var nameSpace = require('continuation-local-storage').getNamespace('nameSpace');
  var traceId = req.get(GLOBAL.com.mdsol.csa.traceIdKey);
  if (!traceId) {
    traceId = uuid.v4();
  }
  res.header('trace_id', traceId);
  nameSpace.run(function() {
    nameSpace.set('trace_id', traceId);
  });
});

Upvotes: 1

Related Questions