开发者

Is there a way to filter output in Google Chrome's console?

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?

EDIT

I found a way to disable the output that was causing the biggest amm开发者_如何学编程ount 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.


You can use regular expressions.

For example to exclude the word browser-sync I use ^((?!browser-sync).)*$.

Is there a way to filter output in Google Chrome's console?

See also here


Chrome 44.0.2403.125


Going further than the above answer comments..

Go in console mode ( Control Shift J on Windows ) , enter this :

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.


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).

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 post for more detail and the JavaScript file:


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


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.

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


If regular expressions aren't your thing, there are two other things that you can do:

  1. In the Filter input box you can add a string of negative matches that will prevent messages beginning with those characters from displaying.

Example:

-WARNING -Errorcode -bonk

Will suppress messages like the following:

WARNING line 234 something else on this line
This message will still display
Errorcode 234: Some error that will be hidden
bonk

The only message from the above that would display is:

This message will still display
  1. ANOTHER simple thing you can do is to right-click on any line in the console log and choose the first option from the pop-up context menu: "Hide messages from ___________" (some script name). This will hide all error messages produced by that script.

To "unhide" those messages again, note that the undesired script was added into the Filter input box - just delete it from there and the messages will return.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜