javascript 有没有办法在 Google Chrome 的控制台中过滤输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6659312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to filter output in Google Chrome's console?
提问by Jorge Guberte
I'm getting a lot of noise from the output of the 3rd party's page i'm currently playing with and i wonder if there's a way to filter the output on the console. Something like Logcat's flags. Is there a way to do that?
我从我目前正在玩的第 3 方页面的输出中得到了很多噪音,我想知道是否有办法过滤控制台上的输出。类似于 Logcat 的标志。有没有办法做到这一点?
EDIT
编辑
I found a way to disable the output that was causing the biggest ammount of noise. I clicked with the right-clicked on the console and then disabled the XMLHttpRequest Logging
option. It's not what i wanted, but it's what i needed.
我找到了一种方法来禁用导致最大噪声量的输出。我在控制台上单击鼠标右键,然后禁用了该XMLHttpRequest Logging
选项。这不是我想要的,但这是我需要的。
回答by Luca Bonavita
回答by tomdemuyt
Going further than the above answer comments..
比上述答案评论更进一步..
Go in console mode ( Control Shift J on Windows ) , enter this :
进入控制台模式(Windows 上的 Control Shift J),输入:
console.nativeLog = console.log;
Then enter this
然后输入这个
console.log = function( a, b ){ if(a=="extension") console.nativeLog( b ) }
The first line keeps the native implementation in a safe spot. The second line does pretty much what you request.
第一行将本地实现保存在安全的位置。第二行几乎可以满足您的要求。
Works for me.
对我来说有效。
回答by Tom McKearney
I just blogged about my solution to this. I modified Ben Alman's "ba-debug" library and made a modular "Trace" object designed to be used with different modules or areas of the code (defined by you).
我刚刚在博客上写了我对此的解决方案。我修改了 Ben Alman 的“ba-debug”库,并制作了一个模块化的“Trace”对象,设计用于不同的模块或代码区域(由您定义)。
Basic usage:
基本用法:
var _trace = new Trace('ModuleName');
Then, when you want to trace out any level of diagnostics, you do:
然后,当您想追踪任何级别的诊断时,您可以:
_trace.error('error level message');
_trace.warn('warning level message');
_trace.info('information level message');
_trace.log('log level message');
_trace.debug('debug level message');
Then, in your page, or in your console, you can do this:
然后,在您的页面或控制台中,您可以执行以下操作:
Trace.traceLevel('ModuleName', Trace.Levels.warn);
Here's my blog postfor more detail and the JavaScript file:
这是我的博客文章以获取更多详细信息和 JavaScript 文件:
回答by mrtsherman
If you have control of both the page and extension scripts then you can run both through your own function. In that function you could now control output.
如果您可以控制页面和扩展脚本,那么您可以通过自己的函数运行两者。在该功能中,您现在可以控制输出。
var pageErrors = true;
var extErrors = true;
function outputToConsole(message, sender) {
if (sender == 'page' && pageErrors) { console.write(message); }
if (sender == 'ext' && extErrors) { console.write(message); }
}
Everywhere you want to log replace console.log with outputToConsole()
您想记录的任何地方都将 console.log 替换为 outputToConsole()
回答by pyramids
This is what I just wrote to solve the same problem. Compared to the previous answers, it has the benefit of properly handling multiple arguments to console.log
and of preventing the absence of window.console.log to throw uncaught exceptions.
这就是我刚刚写的解决同样问题的方法。与之前的答案相比,它的好处是可以正确处理多个参数console.log
并防止缺少 window.console.log 来抛出未捕获的异常。
(function(){
window.console = window.console||{log:0};
var nativeLog = window.console.log;
window.console.log = function() {
try {
// these conditions find all console.log output
// from bitcoinjs-0.1.3 but not much else
// (who else ends an extremely short first parameter with a space?)
if ((arguments.length == 2)
&& (arguments[0].length <= 5)
&& (arguments[0].slice(-2) === ': ')
) {
return;
};
nativeLog.apply(window.console, arguments);
} catch(e) {};
};
})();