Wednesday, November 16th, 2005

log4js: Logging in JavaScript

Category: Library

Stephan Strittmatter has written about adopting Log4J in JavaScript, and came up with log4js.

There are already three appenders to get the output of logging:

  • Write out the logs to a additional popup window in browser with WindowAppender.
  • I was also inspired by Dan Allen Logging via Meta and added an appender using his idea to add logging to meta with MetatagAppender.
  • Default is DummyAppender, which is doing nothing. If you do not set an specific appender, this one is used and nothing is logged. This makes it easy to disable logging by juest commenting out the setAppender() line in your scripts.

Example Code

<!-- first import log4js in your page --> 
<script src="log4js" type="text/javascript" /> 
//create an Logger and set the wanted LogLevel on constructer 
var log = new Logger(LogLevel.TRACE); 
//to log to separate log window set appender to WindowAppender 
log.setAppender(new WindowAppender()); 
//then you can call the log within your page 
log.trace( "trace me" ); 
log.debug( "also debug" );

Posted by Dion Almaer at 12:43 am

3 rating from 5 votes


Comments feed

I’ve added the ability to log to multiple appenders and also added two new appenders to Stephan’s code:

-FileAppender – logs to a text file
-WindowsEventAppender – logs to windows event log

More info here:

Comment by Seth Chisamore — November 16, 2005


Comment by gp03d — December 8, 2005

Leave a comment

You must be logged in to post a comment.