Cannot get log4javascript to work with an InPageAppender when using requirejs

768 Views Asked by At

If I use log4javascript without requirejs, everything (including in-page appenders) works as expected.

When using requirejs, this works (a pop-up window appears with the warning):

var log = log4javascript.getDefaultLogger();
log.warn("This is a test error message.");

but this doesn't work (no in-page area is displayed):

var log4j = log4javascript.getLogger();

var log4jInPageAppender = new log4javascript.InPageAppender();

log4j.addAppender(log4jInPageAppender);

log4j.warn("This is a warning!");

It is shimmed like this:

requirejs.config({
        shim: {'log4javascript': {exports: 'log4javascript'}
    }
});

Everything seems to be defined properly for log4javascript so my log4j and log4jInPageAppender variables are set with valid values.

1

There are 1 best solutions below

0
On BEST ANSWER

The problem is that the InPageAppender relies on log4javascript's own page load handling code to know whether it can start drawing itself. When loaded via RequireJS, the page is already loaded and log4javascript's page load handling code never runs.

You can get around this by calling log4javascript.setDocumentReady(). The most sensible place to do this would seem to be in the shim init() function, but I'm no expert in RequireJS so there may be a better way:

requirejs.config({
    shim: {
        'log4javascript': {
            exports: 'log4javascript',
            init: function() {
                log4javascript.setDocumentReady();
            }
        }
    }
});