I am having trouble debugging my code, and the console.trace function seems to behave weirdly.
I have a code such as:
func1() {
console.trace("hey");
}
func2() {
func1();
}
func2();
The resulting log looks like this:
"hey"
func1 @ script.js:2
This is not really helpful as you can see. What could explain this kind of behaviour ?
In your example,the behavior of console.trace([message][, ...args]) is exactly what is expected
const func1 = () => {
console.trace("hey");
}
const func2 = () => {
func1();
}
func2();
Trace: hey
at func1 (/script.js:2:11)
at func2 (/script.js:6:3)
You can see from the above error, func1 was called by func2.
From this we understand which functions called each other.
We can also see the line number and file that the function exists on
The topmost line is the error message we passed.
The stack trace helps us to know the steps that lead up to our error.
Now we know where our stack trace generated which is in the func1.
That's why func1 is showing at the top. This will make our whole debugging process easier.
Please refer this:
While I'm not sure why console.trace() behaves like this, I did find a workaround as I needed proper stack traces. Simply replacing all console.trace(...) calls with console.log(..., (new Error()).stack) resulted in stack traces working, sometimes with far more detail.