我正在尝试将控制台调用重定向到log4javascript库。

因此,基本上,对console.log的任何调用都会调用log.info,其中log是Log4javascript实例。

但是,当它调用log.info时,出现“功能出席”错误(法语),其基本含义是“预期功能”。

同样,我也尝试从IE8控制台调用log.info

我认为它与脚本无关,但是在这种情况下,它是:

(function (fallback) {

    fallback = fallback || function () { };

    // function to trap most of the console functions from the FireBug Console API.
    var trap = function () {
        // create an Array from the arguments Object
        var args = Array.prototype.slice.call(arguments);
        // console.raw captures the raw args, without converting toString
        console.raw.push(args);
        var message = args.join(' ');
        console.messages.push(message);
        fallback(message);
    };

    // redefine console
    if (typeof console === 'undefined') {
        console = {
            messages: [],
            raw: [],
            dump: function() { return console.messages.join('\n'); },
            log: trap,
            debug: trap,
            info: trap,
            warn: trap,
            error: trap,
            assert: trap,
            clear: function() {
                console.messages.length = 0;
                console.raw.length = 0 ;
            },
            dir: trap,
            dirxml: trap,
            trace: trap,
            group: trap,
            groupCollapsed: trap,
            groupEnd: trap,
            time: trap,
            timeEnd: trap,
            timeStamp: trap,
            profile: trap,
            profileEnd: trap,
            count: trap,
            exception: trap,
            table: trap
        };
    }

})(log.info);


我以为Log4Javascript支持IE8,那么这怎么了?谢谢。

最佳答案

log4javascript确实支持IE8。问题是this在对log.info的调用中是错误的,该调用应被称为方法,因此对log的引用是this的值。我建议通过将logger对象传递给IIFE并调用其info方法来修复它:

(function (log) {
    var fallback = log ?
            function() {
                var args = Array.prototype.slice.call(arguments);
                log.info.apply(log, args);
            } :
            function() { };

    // function to trap most of the console functions from the FireBug Console API.
    var trap = function () {
        // create an Array from the arguments Object
        var args = Array.prototype.slice.call(arguments);
        // console.raw captures the raw args, without converting toString
        console.raw.push(args);
        var message = args.join(' ');
        console.messages.push(message);
        fallback(message);
    };

    // redefine console
    if (typeof window.console === 'undefined') {
        window.console = {
            messages: [],
            raw: [],
            dump: function() { return console.messages.join('\n'); },
            log: trap,
            debug: trap,
            info: trap,
            warn: trap,
            error: trap,
            assert: trap,
            clear: function() {
                console.messages.length = 0;
                console.raw.length = 0 ;
            },
            dir: trap,
            dirxml: trap,
            trace: trap,
            group: trap,
            groupCollapsed: trap,
            groupEnd: trap,
            time: trap,
            timeEnd: trap,
            timeStamp: trap,
            profile: trap,
            profileEnd: trap,
            count: trap,
            exception: trap,
            table: trap
        };
    }
})(log);

关于javascript - log4javascript IE8意外错误“功能出席”/“预期功能”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30530888/

10-11 15:25