log4javascript IE8 意外错误 "Function attendue"/"Expected function"

log4javascript IE8 unexpected error "Function attendue"/"Expected function"

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

所以基本上,任何对 console.log 的调用都会调用 log.infolog 是一个 Log4javascript 实例。

但是当它调用 log.info 时,我得到一个 "Fonction attendue" 错误(法语),这基本上意味着 "Function expected".

我也尝试从 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 确实支持 IE 8。问题是 this 在对 log.info 的调用中不正确,它期望作为方法被调用,因此引用 log 作为 this 的值。我建议通过将记录器对象传递给您的 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);