How to include the trace information while using my own log function?

Viewed 34

I have a custom log function like this.

const myLog = (...args) => {
  const [first, ...rest] = args;
  console.log('%c' + first, 'color:red;', ...rest);
};

function foo() {
  console.log('hello');
  myLog('hello');
}

foo();

Whenever I logout something using this function, the trace information always points to the log function itself, which is super inconvenient that I want to find out which line of code is logging the message.

Is it possible to make it like the native console.log function, that points to the location that myLog is called? (Instead of line 3, I want it to point to line 8)

log

trace

1 Answers

You could use console.trace() instead of log. This will have clickable links to the full stack trace.

console.trace('%c' + first, 'color:red;', ...rest);

Screenshot from jsfiddle:

enter image description here

Related