Intercept console.log but keep stack

我是研究僧i 提交于 2020-01-13 20:29:07

问题


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){return e}");


来源:https://stackoverflow.com/questions/25540308/intercept-console-log-but-keep-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!