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

94 Views Asked by At

I'm trying to redirect console calls to log4javascript library.

So basically, any call to console.log will call log.info, log being a Log4javascript instance.

But when it calls log.info I get a "Fonction attendue" error (French) which basically means "Function expected".

I also tried to call log.info from the IE8 console, same story.

I don't think it's related to the script, but in case of, here it is:

(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);

I thought Log4Javascript supported IE8, so what's wrong here? Thanks.

1

There are 1 best solutions below

2
On BEST ANSWER

log4javascript does support IE 8. The problem is that this is incorrect in the call to log.info, which expects to be called as a method and hence have a reference to log as the value of this. I suggest fixing it by passing in the logger object to your IIFE and calling its info method:

(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);