2

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

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

但是当它调用时,log.info我得到一个“Fonction Attendue”错误(法语),这基本上意味着“预期的功能”。

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

我认为它与脚本无关,但如果有,这里是:

(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,那么这里有什么问题?谢谢。

4

1 回答 1

1

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);
于 2015-05-31T11:26:36.940 回答