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.
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 if you use it during development, you will want to disable it when you go to production.So you could do something like this:
Looks like you're using Winston here - I typically pass
module
into my logger module and then set Winston'slabel
property to a parsed version ofmodule.filename
. Something like:logger.js:
Usage (assume module is
controllers/users.js
):Result:
Assuming each file is a separate node process, you could use something like
process.argv[1].match(/[\w-]+\.js/gi)[0]
If you are looking for something that will work in modules this might work: