views:

48

answers:

1

I'm wondering if anyone happens to have some experience using Log4js? It seems its normal ConsoleAppender isn't always ready to use immediately after it's added to a logger object... If I have two sequential script tags in a document like:

//Initialize logger
<script type="text/javascript">                   
      var logger = new Log4js.getLogger("JSLOG");
      logger.addAppender(new Log4js.ConsoleAppender(logger, false));
      logger.setLevel(Log4js.Level.INFO);         
</script>


//Use logger
<script type="text/javascript">
     logger.info('Test test');
</script>

... It causes the console pop-up (pop-up window) to appear with an error message on page load:

12:58:23 PM WARN Log4js - Could not run the listener function () {
    return fn.apply(object, arguments);
}. 
TypeError: this.outputElement is null

The console is still initialised, it's there afterward, but for just that first logger call it doesn't seem to be there fully. If I make the first logger call setTimeout("logger.info('test test')", 1000), it doesn't have the error. So it's like it's not ready immediately. Anyone see this before or know what a workaround might be?

Cheers

+1  A: 

May I suggest using log4javascript instead? It has a very similar API and a superior console, and is actively being developed. The equivalent in log4javascript would be:

<script type="text/javascript" src="log4javascript.js"></script>

<script type="text/javascript">                   
      //Initialize logger
      var logger = log4javascript.getDefaultLogger();
      logger.setLevel(log4javascript.Level.INFO);         
</script>


<script type="text/javascript">
     //Use logger
     logger.info('Test test');
</script>

I should point out that I am the author of log4javascript.

Tim Down
Hey nice work! I think I may port over, thanks for the link.
perrierism