Can you load the PopupAppender on demand?

144 Views Asked by At

I am trying to use log4javascript and was wondering if there is any way to load the PopupAppender on demand.

I am seeking functionality much like the in-browser tools, where there would be an icon in my application that indicates that something has been logged and when I click it, the PopupAppender opens and allows me to view the logs.

I'm thinking I could write my own very simple appender to show the icon if there are errors, but i'm not sure how I could load up the PopupAppender and show historic messages?

1

There are 1 best solutions below

0
On BEST ANSWER

You'd have to have some kind of proxy appender, as you suggest, which stores logging messages and creates a PopUpAppender on demand. Something like this:

Demo: http://jsfiddle.net/hDRpT/

Code:

function OnDemandPopUpAppender() {
    this.popUpAppender = new log4javascript.PopUpAppender();
    this.poppedUp = false;
    this.popperUpperDisplayed = false;
    this.queuedLoggingEvents = [];
}

var proto = new log4javascript.Appender();
OnDemandPopUpAppender.prototype = proto;

proto.appendQueued = function() {
    for (var i = 0, loggingEvent; loggingEvent = this.queuedLoggingEvents[i++]; ) {
        this.popUpAppender.append(loggingEvent);
    }
    this.queuedLoggingEvents.length = 0;
};

proto.popUp = function() {
    this.poppedUp = true;
    this.appendQueued();
};

proto.append = function(loggingEvent) {
    var appender = this;
    this.queuedLoggingEvents.push(loggingEvent);

    if (this.poppedUp) {
        this.appendQueued();
    } else if (!this.popperUpperDisplayed &&
            loggingEvent.level.isGreaterOrEqual(log4javascript.Level.ERROR)) {
        var popperUpper = document.createElement("div");
        popperUpper.style.border = "solid red 2px";
        popperUpper.innerHTML = "There are error messages in the log. Click to open.";
        popperUpper.onclick = function() {
            appender.popUp();
        }
        document.body.appendChild(popperUpper);
        this.popperUpperDisplayed = true;
    }
};

var log = log4javascript.getLogger("main");
log.addAppender(new OnDemandPopUpAppender());

log.debug("A debug message");
log.error("A horrible error!");