Skip to content

Instantly share code, notes, and snippets.

@mksoni88
Created September 15, 2015 04:10
Show Gist options
  • Save mksoni88/03f69c9c40bb65aa8bbb to your computer and use it in GitHub Desktop.
Save mksoni88/03f69c9c40bb65aa8bbb to your computer and use it in GitHub Desktop.
Override console.log to print line number ( and filename ) (sometimes helpful in nodejs debugging)
_log = console.log;
global.console.log = function() {
var traceobj = new Error("").stack.split("\n")[2].split(":");
var file = traceobj[0].split(process.env.PWD + '/')[1];
var line = traceobj[1];
var new_args = [file + ":" + line + " >>"];
new_args.push.apply(new_args, arguments);
_log.apply(null, new_args);
};
@JCDeen
Copy link

JCDeen commented Feb 21, 2023

This solved my problem perfectly!
Now I just need to understand it all! 👍

@BigPapoo
Copy link

Nice trick but the stack property of en Error object is tagged as "Non-standard". I would check if Error("").stack returns as undefined and work around with a default fallback if it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment