node.jsloggingwinston

I want to display the file Name in the log statement


For every logger statement with any level, I need to display the file name from where the log statement executed, below is the illustration I given below:

Example : Below is the line executed from JobWork.js

logger.info("getInCompleteJobs in job works");

Actual :

2012-11-05T06:07:19.158Z - info: getInCompleteJobs in job works

Required :

2012-11-05T06:07:19.158Z - info JobWork.js : getInCompleteJobs in job works

Without passing the fileName as a parameter from the log statement it should give the filename.


Solution

  • You can use the stack trace information attached to v8's Error object to find out what file/line your code was called from. This approach works well, but it does not perform well; so you will want to disable it in production.

    So you could do something like this:

      var logger_info_old = logger.info;
      logger.info = function(msg) {
        var fileAndLine = traceCaller(1);
        return logger_info_old.call(this, fileAndLine + ":" + msg);
      }
    
      /**
      * examines the call stack and returns a string indicating 
      * the file and line number of the n'th previous ancestor call.
      * this works in chrome, and should work in nodejs as well.  
      *
      * @param n : int (default: n=1) - the number of calls to trace up the
      *   stack from the current call.  `n=0` gives you your current file/line.
      *  `n=1` gives the file/line that called you.
      */
      function traceCaller(n) {
        if (isNaN(n) || n<0) n=1;
        n+=1;
        var s = (new Error()).stack,
            a = s.indexOf('\n', 5);
        while (n--) {
          a = s.indexOf('\n', a+1);
          if (a<0) { 
            a = s.lastIndexOf('\n', s.length);
            break;
          }
        }
        var b = s.indexOf('\n', a+1); 
        if( b<0 ) b = s.length;
        a = Math.max(
          s.lastIndexOf(' ', b),
          s.lastIndexOf('/', b));
        b = s.lastIndexOf(':', b);
        s = s.substring(a+1, b);
        return s;
      }