Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

I want to display the file Name in the log statement #197

Closed
dsaikumar opened this Issue · 8 comments

4 participants

@dsaikumar

For every logger.info or logger.debug, I need to display the filename in which the following line executed.

Example :

JobWorks.js

logger.info("Hello Logger");

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

Required :
2012-11-05T06:07:19.158Z - warn JobWorks : getInCompleteJobs in job works

@dsaikumar

guys please help me, I found a helpful link http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi

@soulraven

subscribe to this, plus my personal suggestion, also display the row from where the log has been executed and from what function name

@dsaikumar

In my previous reply I said about V8 Javascript Stack Trace API, it is right, now I can get the file name also.

I have writte a function in my js file which will return the file name :

function getFileName(){
var fileName="";
var rowNumber;
var columnNumber;
var currentStackPosition=1; // this is the value representing the position of your caller in the error stack.
try{
throw new Error("Custom Error");
}catch(e){
Error["prepareStackTrace"]=function(){
return arguments[1];
};
Error.prepareStackTrace(e, function(){});
fileName=e.stack[currentStackPosition].getFileName();
rowNumber=e.stack[currentStackPosition].getLineNumber();

columnNumber=e.stack[currentStackPosition].getColumnNumber();
}
return {"file":fileName,"row":rowNumber,"column":columnNumber};
}

@dsaikumar dsaikumar closed this
@dsaikumar dsaikumar reopened this
@dsaikumar

I feel this is ok, If any better suggestions are available please provide me.

@mmalecki

This is a really big performance hit. It involves creating an error object and reading the stack trace every time anything is logged. It doesn't belong to winston core. Feel free to create your own logger tho.

@mmalecki mmalecki closed this
@dsaikumar

thanks for the comment, so how else we can get the file name in the logger

@calvinfo

We use a module which wraps around a logger instance. It changes the signature for requiring it.
As a quick example for info:

var logger = require('logger')(module);

logger.info('My log statement');

Inside your logger module:

var winston = require('winston');

module.exports = function (module) {

  var filename = module.id;
  return {
    info : function (msg, vars) { 
      winston.info(filename + ': ' + msg, vars); 
    }
  }
};

We made it a little more generic to handle other levels of logging, but the basic idea is still the same.

Agree though that it probably doesn't belong in winston source.

@soulraven

way, my opinion is that is a good feature, to be ported in winston as a switch element for logging.
at least this feature, if the posibilty to display the row and the method from where the logging is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.