Skip to content

Instantly share code, notes, and snippets.

@rvwhitney
Last active February 15, 2021 03:17
Show Gist options
  • Save rvwhitney/d03526fe86ceacaaefe5225df40e48e6 to your computer and use it in GitHub Desktop.
Save rvwhitney/d03526fe86ceacaaefe5225df40e48e6 to your computer and use it in GitHub Desktop.
Show Line # the console.log is on for Node.js server
// can't remember where I got this but I did not write it:
// if app requires db.js
// place it in a required file, like db.js and the variable '__line__' will be available in app as well
// begin Object for __line__
Object.defineProperty(global, '__stack__', {
get: function(){
var orig = Error.prepareStackTrace;
Error.prepareStackTrace = function(_, stack){ return stack; };
var err = new Error;
Error.captureStackTrace(err, arguments.callee);
var stack = err.stack;
Error.prepareStackTrace = orig;
return stack;
}
});
Object.defineProperty(global, '__line__', {
get: function(){
return __stack__[1].getLineNumber();
}
});
// end Object for __line__
// place the following into every file you want logs for
// I did write the following:
function log(line, a, b, c, d) {
var time = new Date();
b = b || '';
c = c || '';
d = d || '';
var year,mon,day,hour,min,sec, msec;
year = time.getFullYear()+'-';
mon = ("0" + (time.getMonth()+1)).slice(-2)+'-';
day = ("0" + time.getDate()).slice(-2)+' ';
hour = ("0" + time.getHours()).slice(-2)+':';
min = ("0" + time.getMinutes()).slice(-2)+':';
sec = ("0" + time.getSeconds()).slice(-2)+':';
msec = ("00" + time.getMilliseconds()).slice(-3);
console.log(year+mon+day+hour+min+sec+msec,"::: NAME OF FILE YOU ARE IN ("+line+")" , a, b, c, d);
}
// Usage: log(__line__, var1, var2, var3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment