I want to make a logging mechanism that also includes the file that the log statement was written from.
So for instance, if I have a file:
//foo.js
log("stuff");
Then I want the log
function to be able to include the file name "foo.js"
in the logs.
Is this possible? I haven't been able to find anything about it anywhere.
You can trigger a new error and catch it internally. That way, you can access the stack trace using myError.stack
, which will print a list of files that have led to your execution point. Example:
try {
throw new Error('Trace!');
} catch (err) {
console.log(err.trace);
}
If you have more questions, let me know!