I have a helper function that prepends some message when calling console.log
:
function log(...arg: any[]): void {
const LOG_PREFIX = `[file.ts]`;
// 1. Convert args to a normal array
const args: any[] = Array.prototype.slice.call(arguments);
// 2. Prepend log prefix log string
args.unshift(LOG_PREFIX);
// 3. Pass along arguments to console.log
console.log.call(console, ...args);
}
The problem is that when it logs, the console shows a clickable link to this log:8
or the line that calls console.log.call(console, ...args)
Is there a way I can adjust this method so that it keeps the intended stack trace of where i call this method instead?
CodePudding user response:
There you go, Function.prototype.bind()
is to the rescue:
const log = console.log.bind(window.console, "[file.ts]");
log("test", "blah");
console.log("test", "blah");