Wednesday, November 16th, 2005

log4js: Logging in JavaScript

Category: Library

<>p>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" );

Related Content:

Posted by Dion Almaer at 12:43 am
2 Comments

+++--
3 rating from 5 votes

2 Comments »

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:
http://issues.apache.org/bugzilla/show_bug.cgi?id=36961

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.