Intercept console.log but keep stack
问题 I know its easy to intercept a function in js, among other ways: console.log = (function () { var log = console.log; return function () { alert(arguments); log.apply(console, arguments); })(); but Is there a way to wrap console.log such that when a user calls console.log("hi")//in random.js in the console it shows the random.js origin, and not the location of the intercept? 回答1: Use a try/catch rather than returning a function: console.log = Function("a", "try { console.info(a); } catch(e)