26

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.

tomdemuyt
  • 4,572
  • 2
  • 31
  • 60
Jorge Guberte
  • 10,464
  • 8
  • 37
  • 56
  • Filter in what way? Like by site? – Blender Jul 12 '11 at 03:57
  • I'm developing an extension which outputs to the console, and the page also outputs to the console... i'd like to do something like `console.log('extension', 'The message');` and filter the output so i only see stuff flagged with 'extension' instead of the whole output. EDIT Or filter by levels: `console.warn()`, `console.error()` – Jorge Guberte Jul 12 '11 at 03:59
  • I see. I don't think that functionality exists in Chrome... – Blender Jul 12 '11 at 04:02
  • As for Warn and Error, if you're running the dev channel, I have the filter buttons on the bottom pane: http://i.imgur.com/0VN67.png – Blender Jul 12 '11 at 04:03
  • That's not the ideal solution because the noise spans all log levels, but it'll have to do for now. I'm trying to find an extension to help me on that. – Jorge Guberte Jul 12 '11 at 04:06
  • In Firefox, extensions can access virtually every part of the browser. In Chrome, it's a lot more restricted. I don't think a Chrome extension can modify the Developer Tools thing... – Blender Jul 12 '11 at 04:08
  • I was thinking about an extension that has the flagging feature. :) – Jorge Guberte Jul 12 '11 at 04:22
  • 1
    You can search for a specific text in your error messages, using the search box in the top right corner of the console. – NoBugs Jul 21 '11 at 00:21
  • 1
    Does this answer your question? [Filter the Chrome console messages](https://stackoverflow.com/questions/44130085/filter-the-chrome-console-messages) – SuperStormer May 23 '23 at 05:37

6 Answers6

23

You can use regular expressions.

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

enter image description here

See also here


Chrome 44.0.2403.125

Community
  • 1
  • 1
mindrones
  • 1,173
  • 11
  • 15
  • 3
    Since the checkbox `Regex` has been removed from the latest chrome, I used `/^((?!browser-sync).)*$/` to indicate that it is a regular expression. Taken from [this answer](https://stackoverflow.com/a/42044616/6053299). – margaretkru Jul 05 '17 at 14:35
  • 1
    On recent version you can just prefix the word to be negated with a `-`, see https://developers.google.com/web/updates/2017/08/devtools-release-notes#negative-filters – pcworld Sep 26 '19 at 15:20
22

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.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
tomdemuyt
  • 4,572
  • 2
  • 31
  • 60
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:

Sudarshan
  • 18,140
  • 7
  • 53
  • 61
Tom McKearney
  • 315
  • 2
  • 11
2

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

mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • I don't have control of the page, unfortunately. – Jorge Guberte Jul 12 '11 at 04:19
  • 1
    Hmmm, got me thinking. Check this out on SO. This guy turns logging on and off and even has an example. http://stackoverflow.com/questions/1215392/how-to-quickly-and-conveniently-disable-all-console-log-statements-in-my-code – mrtsherman Jul 12 '11 at 04:34
  • Thanks for my first bounty! Any reason you did not flag my post as the answer ? – tomdemuyt Aug 01 '11 at 20:39
1

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.

cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

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) {};
    };
})();
  • I have no idea why someone had downwoted you, based on your answer I was able to create a nice filter for too verbose third party library that spammed the console. Sure, the try-catch block could be a bit more clear what it does, but the rest of it was golden. – Mikko Tapionlinna Jan 16 '15 at 07:44
  • You can probably omit the exception handling (the try and catch lines), but there is a risk of accidentally breaking code. If any of the code you insert, for example the matching itself, ever runs into an exception that the caller of console.log sees, you do something that calling code was not written to expect. –  Jan 16 '15 at 21:02