log4es - Logging For EcmaScript

It turned out that the log4js library we’d started adopting does not support globally configured appenders, but rather expected us to configure appenders for each individual logger. Also, it’s rather big, unwieldy and has more levels of indirection than you really want in a tight loop in javascript.

We therefore went ahead and prettied up the logging library that Árni had worked on, re-naming it to log4es to distinguish it from the two existing log4js libraries.

This small testbed shows how it’s used:

// import tv.zignal.log4es.LogFactory
// import tv.zignal.core.managers.core.ZignalServerLogAppender
// import tv.zignal.integration.browser.FirebugLogAppender

var logger = LogFactory.getLogger("tv.zignal.testbed");
logger.info("Doesn't show up");

LogFactory.addAppender(new ZignalServerLogAppender(Logger.WARN));
LogFactory.addAppender(new FirebugLogAppender(Logger.DEBUG));

logger.info("Now in full colour (on the client side)");
logger.warn("Worry (on the server side)");
logger.error("Panic! (on the server side)");

try {

    function bar() {
        var x;
        x.smu();
    }

    function foo() {
        bar();
    }

    foo();

} catch (e) {
    logger.warn("exception and stack-trace!", e);
}

It results in this being logged in the firebug console:

(i) tv.zignal.testbed | Now in full colour (on the client side)jsload (line 1592)
(!) tv.zignal.testbed | Worry (on the server side)jsload (line 1592)
(x) tv.zignal.testbed | Panic! (on the server side)jsload (line 1592)
(!) tv.zignal.testbed | exception and stack-trace! | TypeError: x has no properties at http://localhost:8080/launch/jsload?group=testbed:1619
        bar()@http://localhost:8080/launch/jsload?group=testbed:1619
        foo()@http://localhost:8080/launch/jsload?group=testbed:1623
        @http://localhost:8080/launch/jsload?group=testbed:1626

and this on the server (after removing the thread name and CoreClientApi.java:75):

22:35:57.200 ERROR | [265829:70:166732] tv.zignal.testbed | Panic! (on the server side)
22:35:57.235 WARN  | [265829:70:166732] tv.zignal.testbed | Worry (on the server side)
22:35:57.247 WARN  | [265829:70:166732] tv.zignal.testbed | exception and stack-trace! | TypeError: x has no properties at http://localhost:8080/launch/jsload?group=testbed:1619
    bar()@http://localhost:8080/launch/jsload?group=testbed:1619
    foo()@http://localhost:8080/launch/jsload?group=testbed:1623
    @http://localhost:8080/launch/jsload?group=testbed:1626

Current appender implementations are:

  • AlertLogAppender which delegates to the alert function. This is usually extremely annoying, but will log to the console in the ekioh browser running on a desktop computer.
  • AminoLogAppender which logs to the system log on Amino boxes in Opera.
  • FirebugLogAppender which logs to the firebug console (or any other global console object with debug, info, warn and error methods)
  • ZignalServerLogAppender which sends log lines to the Zignal Server to be logged there.

Updated: added list of appender implementations.

Leave a Reply

You must be logged in to post a comment.