Created
February 4, 2016 06:49
-
-
Save dezinezync/ededcf96e379fd2ddff3 to your computer and use it in GitHub Desktop.
Log Line Numbers in NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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