Skip to content

Instantly share code, notes, and snippets.

@gerrard00
Created February 4, 2019 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gerrard00/ac7f25bb41d469144ce3d7115fd74a14 to your computer and use it in GitHub Desktop.
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
// 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