Created
February 4, 2019 18:09
-
-
Save gerrard00/ac7f25bb41d469144ce3d7115fd74a14 to your computer and use it in GitHub Desktop.
My version of code to include line numbers in console logs for node js
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
// https://stackoverflow.com/questions/45395369/how-to-get-console-log-line-numbers-shown-in-nodejs | |
['log','warn','error'].forEach((methodName) => { | |
const originalMethod = console[methodName]; | |
console[methodName] = (...args) => { | |
const error = new Error(); | |
originalMethod.apply( | |
console, | |
[ | |
( | |
error | |
.stack // Grabs the stack trace | |
.split('\n')[2] // Grabs third line | |
.trim() // Removes spaces | |
.substring(3) // Removes three first characters ("at ") | |
.replace(__dirname, '') // Removes script folder path | |
.replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at " | |
.replace(/\)/, '') // Removes last parentheses | |
), | |
'\n', | |
...args | |
] | |
); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment