Is there a way to filter output in Google Chrome&#

2020-05-18 11:30发布

问题:

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

回答1:

You can use regular expressions.

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

See also here


Chrome 44.0.2403.125



回答2:

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.



回答3:

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:



回答4:

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



回答5:

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