2

コンソール呼び出しを log4javascript ライブラリにリダイレクトしようとしています。

したがって、基本的に、console.logを呼び出すとlog.infologLog4javascript インスタンスである が呼び出されます。

しかし、それが呼び出されるlog.infoと、基本的に「関数が必要です」という意味の「関数参加」エラー (フランス語) が表示されます。

log.infoIE8コンソールからも呼び出してみましたが、同じ話です。

スクリプトとは関係ないと思いますが、場合によっては次のとおりです。

(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 に答える