How do I print debug messages in the Google Chrome JavaScript Console?

前端 未结 14 823
清酒与你
清酒与你 2020-11-27 08:54

How do I print debug messages in the Google Chrome JavaScript Console?

Please note that the JavaScript Console is not the same as the JavaScript Debugger; they have

相关标签:
14条回答
  • 2020-11-27 09:34
    console.debug("");
    

    Using this method prints out the text in a bright blue color in the console.

    0 讨论(0)
  • 2020-11-27 09:35

    Just a quick warning - if you want to test in Internet Explorer without removing all console.log()'s, you'll need to use Firebug Lite or you'll get some not particularly friendly errors.

    (Or create your own console.log() which just returns false.)

    0 讨论(0)
  • 2020-11-27 09:35

    Or use this function:

    function log(message){
        if (typeof console == "object") {
            console.log(message);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 09:36

    Improving on Andru's idea, you can write a script which creates console functions if they don't exist:

    if (!window.console) console = {};
    console.log = console.log || function(){};
    console.warn = console.warn || function(){};
    console.error = console.error || function(){};
    console.info = console.info || function(){};
    

    Then, use any of the following:

    console.log(...);
    console.error(...);
    console.info(...);
    console.warn(...);
    

    These functions will log different types of items (which can be filtered based on log, info, error or warn) and will not cause errors when console is not available. These functions will work in Firebug and Chrome consoles.

    0 讨论(0)
  • 2020-11-27 09:36

    I've had a lot of issues with developers checking in their console.() statements. And, I really don't like debugging Internet Explorer, despite the fantastic improvements of Internet Explorer 10 and Visual Studio 2012, etc.

    So, I've overridden the console object itself... I've added a __localhost flag that only allows console statements when on localhost. I also added console.() functions to Internet Explorer (that displays an alert() instead).

    // Console extensions...
    (function() {
        var __localhost = (document.location.host === "localhost"),
            __allow_examine = true;
    
        if (!console) {
            console = {};
        }
    
        console.__log = console.log;
        console.log = function() {
            if (__localhost) {
                if (typeof console !== "undefined" && typeof console.__log === "function") {
                    console.__log(arguments);
                } else {
                    var i, msg = "";
                    for (i = 0; i < arguments.length; ++i) {
                        msg += arguments[i] + "\r\n";
                    }
                    alert(msg);
                }
            }
        };
    
        console.__info = console.info;
        console.info = function() {
            if (__localhost) {
                if (typeof console !== "undefined" && typeof console.__info === "function") {
                    console.__info(arguments);
                } else {
                    var i, msg = "";
                    for (i = 0; i < arguments.length; ++i) {
                        msg += arguments[i] + "\r\n";
                    }
                    alert(msg);
                }
            }
        };
    
        console.__warn = console.warn;
        console.warn = function() {
            if (__localhost) {
                if (typeof console !== "undefined" && typeof console.__warn === "function") {
                    console.__warn(arguments);
                } else {
                    var i, msg = "";
                    for (i = 0; i < arguments.length; ++i) {
                        msg += arguments[i] + "\r\n";
                    }
                    alert(msg);
                }
            }
        };
    
        console.__error = console.error;
        console.error = function() {
            if (__localhost) {
                if (typeof console !== "undefined" && typeof console.__error === "function") {
                    console.__error(arguments);
                } else {
                    var i, msg = "";
                    for (i = 0; i < arguments.length; ++i) {
                        msg += arguments[i] + "\r\n";
                    }
                    alert(msg);
                }
            }
        };
    
        console.__group = console.group;
        console.group = function() {
            if (__localhost) {
                if (typeof console !== "undefined" && typeof console.__group === "function") {
                    console.__group(arguments);
                } else {
                    var i, msg = "";
                    for (i = 0; i < arguments.length; ++i) {
                        msg += arguments[i] + "\r\n";
                    }
                    alert("group:\r\n" + msg + "{");
                }
            }
        };
    
        console.__groupEnd = console.groupEnd;
        console.groupEnd = function() {
            if (__localhost) {
                if (typeof console !== "undefined" && typeof console.__groupEnd === "function") {
                    console.__groupEnd(arguments);
                } else {
                    var i, msg = "";
                    for (i = 0; i < arguments.length; ++i) {
                        msg += arguments[i] + "\r\n";
                    }
                    alert(msg + "\r\n}");
                }
            }
        };
    
        /// <summary>
        /// Clever way to leave hundreds of debug output messages in the code,
        /// but not see _everything_ when you only want to see _some_ of the
        /// debugging messages.
        /// </summary>
        /// <remarks>
        /// To enable __examine_() statements for sections/groups of code, type the
        /// following in your browser's console:
        ///       top.__examine_ABC = true;
        /// This will enable only the console.examine("ABC", ... ) statements
        /// in the code.
        /// </remarks>
        console.examine = function() {
            if (!__allow_examine) {
                return;
            }
            if (arguments.length > 0) {
                var obj = top["__examine_" + arguments[0]];
                if (obj && obj === true) {
                    console.log(arguments.splice(0, 1));
                }
            }
        };
    })();
    

    Example use:

        console.log("hello");
    

    Chrome/Firefox:

        prints hello in the console window.
    

    Internet Explorer:

        displays an alert with 'hello'.
    

    For those who look closely at the code, you'll discover the console.examine() function. I created this years ago so that I can leave debug code in certain areas around the product to help troubleshoot QA/customer issues. For instance, I would leave the following line in some released code:

        function doSomething(arg1) {
            // ...
            console.examine("someLabel", arg1);
            // ...
        }
    

    And then from the released product, type the following into the console (or address bar prefixed with 'javascript:'):

        top.__examine_someLabel = true;
    

    Then, I will see all of the logged console.examine() statements. It's been a fantastic help many times over.

    0 讨论(0)
  • 2020-11-27 09:45

    Here's my console wrapper class. It gives me scope output as well to make life easier. Note the use of localConsole.debug.call() so that localConsole.debug runs in the scope of the calling class, providing access to its toString method.

    localConsole = {
    
        info: function(caller, msg, args) {
            if ( window.console && window.console.info ) {
                var params = [(this.className) ? this.className : this.toString() + '.' + caller + '(), ' + msg];
                if (args) {
                    params = params.concat(args);
                }
                console.info.apply(console, params);
            }
        },
    
        debug: function(caller, msg, args) {
            if ( window.console && window.console.debug ) {
                var params = [(this.className) ? this.className : this.toString() + '.' + caller + '(), ' + msg];
                if (args) {
                    params = params.concat(args);
                }
                console.debug.apply(console, params);
            }
        }
    };
    
    someClass = {
    
        toString: function(){
            return 'In scope of someClass';
        },
    
        someFunc: function() {
    
            myObj = {
                dr: 'zeus',
                cat: 'hat'
            };
    
            localConsole.debug.call(this, 'someFunc', 'myObj: ', myObj);
        }
    };
    
    someClass.someFunc();
    

    This gives output like so in Firebug:

    In scope of someClass.someFunc(), myObj: Object { dr="zeus", more...}
    

    Or Chrome:

    In scope of someClass.someFunc(), obj:
    Object
    cat: "hat"
    dr: "zeus"
    __proto__: Object
    
    0 讨论(0)
提交回复
热议问题