Chrome debugger - how to turn off console.log message grouping?

后端 未结 4 1225
野趣味
野趣味 2020-12-09 08:14

Say, in my Google Chrome extension I do this:

console.log(msg);

and the Chrome debugger groups similar messages like so:

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 09:14

    Messages are only collapsed with the previous if they are identical.
    To prevent messages from being collapsed, you can either alternate the log levels, or use alternate the log output.

    console.log and console.debug are visually similar in Chrome's devtools (i.e. there is no icon in front of it). If you don't use the verbosity filter, then alternating between console.log and console.debug will solve your problem:

    console.log('message');
    console.debug('message');
    console.log('message');
    
    // Convenience function:
    function log() {
        log.counter = log.counter ? log.counter + 1 : 1;
        console[log.counter % 2 ? 'log' : 'debug'].apply(console, arguments);
    }
    

    The other way to get the desired result is to insert an invisible character in front of the message (note: I use %s to prevent an extra space from appearing (see devtools formatting options), and also a ZWSP to prevent any visual character from appearing at all):

    function log() {
        log.counter = log.counter ? log.counter + 1 : 1;
        var args = [].slice.call(arguments);
        if (log.counter % 2) {
            args.unshift('%s\u200B'); // ZWSP (zero-width space, you won't see it)
        }
        console.log.apply(console, args);
    }
    

    Demo: http://jsfiddle.net/x3725j38/1/

提交回复
热议问题