Skip to content

Instantly share code, notes, and snippets.

@dezinezync
Created February 4, 2016 06:49
Show Gist options
  • Select an option

  • Save dezinezync/ededcf96e379fd2ddff3 to your computer and use it in GitHub Desktop.

Select an option

Save dezinezync/ededcf96e379fd2ddff3 to your computer and use it in GitHub Desktop.
Log Line Numbers in NodeJS
// include it in the first file that NodeJS will load
// yes, eval is dangerous, but not when you know exactly what it's loading
var fs = require("fs");
eval(fs.readFileSync(__dirname+'log.js')+'');
// Usage, in say /routes/user.js
console.log(__line, user.userID)
// /routes/user.js:34:22 > 2
Object.defineProperty(global, '__line', {
get: function(){
return ((new Error()).stack.split("\n")[2].trim().replace(/^(at\s?)(.*)/gim, "$2 >").replace(__dirname, ""))
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment