This page last changed on Oct 19, 2007 by sgs@mcmaster.ca.


N.B. This document has been superceded by the Monk Workbench Documentation

Javascript Logging Library / Java Servlet

Overview:

Java / Javascript library to handle the logging of activity in a web-based environment.

The library uses the 'prototype.js' (http://www.prototypejs.org/) javascript library to handle the AJAX server calls (this was an easy way to get really good, long lasting cross browser support for communication with a server).

Download Link:

The entire package is available as a '.zip' archive (634KB) from:

http://www.digital-profiles.com/MONK_Workbench/MonkLogger.zip

Package Contents:

'Create Log Table.sql'

This file contains SQL commands to create a table to store log data into. The created table will be named 'Logs', and it contains 7 fields:

Field Name Data Type Description
TransactionID text Records the transaction ID (the ID of the send request) for a log message. The transaction ID is used to identify lost log messages and could also be used to identify at a later date which logs came in together. To generate unique transaction ids, the logger combines the millisecond time with the name of the user sending the batch of log messages (e.g. 'UserName-74953985938').
TimeStamp bigint(20) Holds the millisecond time that the message was recieved.
EventType text Holds what type of event was encountered (e.g. 'CheckpointEvent' vs. 'UIEvent'). The event type can be used to quickly distinguish between trivial UI events (e.g. using a scroll bar) and more meaningful user inputs that would be useful in recreating a set of circumstances (e.g. saving a new workset).
UserID text Holds the ID of the user that the event is associatesd with.
EventDispatcher text The component/widget that sent the event.
EventDescription text A description of the event.
EventArguments text Any arguments that relate to the event.

'logger.html'

This is a sample web page that displays a few buttons that fire log messages. This page also demonstrates the use of the logger debug div (named 'loggerDebug'). This gives us a way to output debug / status messages into any page that is using the logger. For debugging to work, it must be enabled in the 'logger.js' file (set the constant 'var debugEnabled' which can be found near the top of 'logger.js' to true).

'logger.js'

This is the javascript library that handles the logging of events. This file depends on the 'prototype.js' javascript library. There are several global settings that can be adjusted in the 'logger.js':

Constant Description
var debugEnabled = true; Output debug messages to the "loggerDebug" div?
var loggingEnabled = true; Is logging enabled?
var serverAddress = "logManager.SaveLogs"; Address of the servlet.
var maximumStackSize = 15; Once we have this many logs, push them to the server.
var timeTillUpload = 5000; Once this much time has elapsed (in ms), send the logs to the server wheather or not we have reached the maximum stack size.
var failureCount = 0; Holds the number of failures that we have encountered (resets to 0 after any successful upload).
var failureLimit = 5; After this many consecutive failures, the logging will cease.

'WEB-INF/src/LogEntry.java'

Java class to represent a log entry. The java class also has a connivence method to export to MySQL:

Method Description
toSQL() Returns a string insert statement that can be used to add the entry to a MySQL database.

It is also possible to create a java log entry from a Java MySQL ResultSet by using the public LogEntry (ResultSet rs) constructor.

'WEB-INF/src/SaveLogs.java'

This is the Java servlet that handles the saving of data to a MySQL database.

Being very dependent on the local MySQL setup, without modification the servlet does not actually save anything, but it does create 'LogEntry' Java objects that contains a public method 'toSQL()'. This method generates a MySQL insert statement that could be used to add the entry to the database.

The servlet takes two arguments:

  1. Messages - A list of URL encoded log messages, separated by the '|' character. The messages are encoded for URL's automatically, but should be in the format: TimeStamp:EventType-UserID,EventDispatcher,EventDescription,EventArguments
  1. TransactionID - A unique identifier to keep track of which log entries came in together. These are automatically generated in the format: UserID-TimeStamp

Example Usage:

www.example.com/logManager.SaveLogs?TransactionID=UserID-1187834569242&Messages=1187834569240:EventType-UserID,EventDispatcher,EventDescription,EventArgs|1187834569241:EventType2-UserID,EventDispatcher2,EventDescription2,EventArgs2

Example Result:

<SaveResult TransactionID="UserID-1187834569242" NumSaved="2">
<SavedMessage TimeStamp="1187834569240"/>
<SavedMessage TimeStamp="1187834569241"/>
</SaveResult>

'WEB-INF/lib/mysql-connector-java-5.0.7-bin.jar'

MySQL connector library for the Java servlet.

'WEB-INF/lib/servlet-api.jar'

Java servlet library.

'WEB-INF/web.xml'

Apache Tomcat servlet mapping. The saving servlet is mapped to 'logManager.SaveLogs'.

Installation:

To compile the Java servlet:

javac -classpath lib/servlet-api.jar:lib/mysql-connector-java-5.0.7-bin.jar. src/*.java -d classes

Usage:

Logger Usage:

Include both the 'prototype.js' and the 'logger.js' javascript files in your web page:

<head>
<script language="JavaScript" SRC="./logger.js"></script>
<script language="JavaScript" SRC="./prototype.js"></script>
</head>

To Log an Event:

Call the javascript function:

log (userID, eventDispatcher, eventDescription, eventArguments, eventType);

The logs will remain in a local javascript array until one of three conditions are met:

  1. The number of logs in the queue exceeds the constant 'var maximumStackSize'.
  2. The elapsed time since the last activity has exceeded the constant 'var timeTillUpload'.
  3. You explicitly call 'flushLogs ();'.

Error Handling:

If an entire transaction fails to successfully upload to the server (i.e. the XML result indicates that NumSaved = 0), it is counted as a failure. After the constant 'var failureLimit' number of failures, the logger will disable itself.

If a transaction partially fails (i.e. the XML result indicates that the number saved was less than the number sent), the logger will attempt to resend the lost entries.

Thanks for putting this here Mike.

Posted by amitku at Aug 22, 2007 23:14
Document generated by Confluence on Apr 19, 2009 15:04