拦截console.log但保持堆栈 [英] Intercept console.log but keep stack

查看:258
本文介绍了拦截console.log但保持堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道很容易拦截js中的一个函数,除其他外:

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);
})();

但有没有办法包装console.log,以便当用户调用时

but Is there a way to wrap console.log such that when a user calls

console.log("hi")//in random.js

在控制台中显示random.js原点,而不是截距的位置?

in the console it shows the random.js origin, and not the location of the intercept?

推荐答案

使用 try / catch 而不是返回一个函数:

Use a try/catch rather than returning a function:

console.log = Function("a", "try { console.info(a); } catch(e){return e}");

这篇关于拦截console.log但保持堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆