log4js.js
Summary
Javascript Logger (in the spirit of log4j)
This library is designed to make the writing and debugging
of javascript code easier, by allowing the programmer to perform
debug or log output at any place in their code. This supports
the concept of different levels of logging (debug < info < warn < error < fatal << none)
as well as different log outputs. Three log outputs are included, but you can
add your own. The included log outputs are Log.writeLogger(),
Log.alertLogger(), and Log.popupLogger(). For debugging on Safari,
the log ouput Log.consoleLogger() is also included. To turn off debugging
but still leave the logger calls in your script, use the log level Log.NONE.
Example usage:
<html>
<head>
<script src="log4js.js" type="text/javascript"></script>
</head>
<body>
Log4JS test...<hr/>
<script>
// Setup log objects
//
// log object of priority debug and the popup logger
var log = new Log(Log.DEBUG, Log.popupLogger);
// log object of priority warn and the alert logger
var log2 = new Log(Log.WARN, Log.alertLogger);
// log object of priority debug and the console logger (Safari)
var log3 = new Log(Log.DEBUG, Log.consoleLogger);
log.debug('foo1'); // will popup a new window and log 'foo'
log.warn('bar1'); // will add a new 'bar' message to the popup
log2.debug('foo2'); // will do nothing (Log object's priority threshold is WARN)
log2.warn('bar2'); // will display a javascript alert with the string 'bar'
log3.debug('foo3'); // will log message to Safari console or existing popup
log3.warn('bar3'); // same
log.info(Log.dumpObject(new Array('apple','pear','orange','banana')));
</script>
</body>
</html>
Version: 0.3
*************************************************************
Copyright 2005 Fourspaces Consulting, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
*************************************************************
Changelog:
0.2 - Added consoleLogger for Safari
- Changed popupLogger so that it only notifies once (or twice)
that a popup blocker is active.
- Added Log.NONE level for silencing all logging
Author: Marcus R Breese mailto:mbreese@fourspaces.com
Class Summary
|
Log |
The main Log class. |
function Log(level,logger,prefix) {
var _currentLevel = Log.WARN;
var _logger = Log.writeLogger;
var _prefix = false;
this.setPrefix = function(pre) {
if (pre!='undefined') { _prefix = pre; }
else { _prefix = false; }
}
this.setLogger = function(logger) {
if (logger!='undefined') { _logger = logger; }
}
this.setLevel = function(level) {
if (level!='undefined' && typeof level =='number') {
_currentLevel = level;
} else if (level!='undefined') {
if (level=='debug') { _currentLevel = Log.DEBUG; }
else if (level=='info') { _currentLevel = Log.INFO; }
else if (level=='error') { _currentLevel = Log.ERROR; }
else if (level=='fatal') { _currentLevel = Log.FATAL; }
else if (level=='warn') { _currentLevel = Log.WARN; }
else { _currentLevel = Log.NONE; }
}
}
this.getPrefix = function() { return _prefix; }
this.getLogger = function() { return _logger; }
this.getLevel = function() { return _currentLevel; }
if (level!='undefined') { this.setLevel(level); }
if (logger!='undefined') { this.setLogger(logger); }
if (prefix!='undefined') { this.setPrefix(prefix); }
}
Log.prototype.debug = function(s) { if (this.getLevel()<=Log.DEBUG) { this._log(s,"DEBUG",this); } }
Log.prototype.json = function(s) {
if (Zapatec && Zapatec.Transport) {
this.debug(Zapatec.Transport.serializeJsonObj(s));
}
}
Log.prototype.info = function(s) { if (this.getLevel()<=Log.INFO ) { this._log(s,"INFO",this); } }
Log.prototype.warn = function(s) { if (this.getLevel()<=Log.WARN ) { this._log(s,"WARN",this); } }
Log.prototype.error = function(s) { if (this.getLevel()<=Log.ERROR) { this._log(s,"ERROR",this); } }
Log.prototype.fatal = function(s) { if (this.getLevel()<=Log.FATAL) { this._log(s,"FATAL",this); } }
Log.prototype._log = function(msg,level,obj) {
if (this.getPrefix()) {
this.getLogger()(this.getPrefix()+" - "+msg,level,obj);
} else {
this.getLogger()(msg,level,obj);
}
}
Log.DEBUG = 1;
Log.INFO = 2;
Log.WARN = 3;
Log.ERROR = 4;
Log.FATAL = 5;
Log.NONE = 6;
Log.alertLogger = function(msg,level) { alert(level+" - "+msg); }
Log.writeLogger = function(msg,level) { document.writeln(level+" - "+msg+"<br/>"); }
Log.consoleLogger = function(msg,level,obj) {
if (window.console) {
window.console.log(level+" - "+msg);
} else {
Log.popupLogger(msg,level,obj);
}
}
Log.popupLogger = function(msg,level,obj) {
if (obj.popupBlocker) {
return;
}
if (!obj._window || !obj._window.document) {
obj._window = window.open("",'logger_popup_window');
if (!obj._window) { obj.popupBlocker=true; alert("You have a popup window manager blocking the log4js log popup display.\n\nThis must be disabled to properly see logged events."); return; }
if (!obj._window.document.getElementById('loggerTable')) {
obj._window.document.writeln("<table width='100%' id='loggerTable'><tr><th align='left'>Time</th><th width='100%' colspan='2' align='left'>Message</th></tr></table>");
obj._window.document.close();
}
}
var tbl = obj._window.document.getElementById("loggerTable");
var row = tbl.insertRow(1);
var cell_1 = row.insertCell(-1);
var cell_2 = row.insertCell(-1);
var cell_3 = row.insertCell(-1);
var d = new Date();
var h = d.getHours();
if (h<10) { h="0"+h; }
var m = d.getMinutes();
if (m<10) { m="0"+m; }
var s = d.getSeconds();
if (s<10) { s="0"+s; }
var date = (d.getMonth()+1)+"/"+d.getDate()+"/"+d.getFullYear()+" - "+h+":"+m+":"+s;
cell_1.style.fontSize="8pt";
cell_1.style.fontWeight="bold";
cell_1.style.paddingRight="6px";
cell_2.style.fontSize="8pt";
cell_3.style.fontSize="8pt";
cell_3.style.whiteSpace="nowrap";
cell_3.style.width="100%";
if (tbl.rows.length % 2 == 0) {
cell_1.style.backgroundColor="#eeeeee";
cell_2.style.backgroundColor="#eeeeee";
cell_3.style.backgroundColor="#eeeeee";
}
cell_1.innerHTML=date
cell_2.innerHTML=level;
cell_3.innerHTML=msg;
}
Log.dumpObject=function (obj,indent) {
if (!indent) { indent="";}
if (indent.length>20) { return ; }
var s="{\n";
for (var p in obj) {
s+=indent+p+":";
var type=typeof(obj[p]);
type=type.toLowerCase();
if (type=='object') {
s+= Log.dumpObject(obj[p],indent+"----");
} else {
s+= obj[p];
}
s+="\n";
}
s+=indent+"}";
return s;
}
Log.zpdumpObject_config=function(objArgs) {
config={}
config.indent=" "
config.depth=1;
config.maxdepth=10;
config.objSkip={}
config.objSkip['function']=true
config.bShowValues=true
config.bShowType=true
config.bSkipEmpty=true
config.bSkipUndefined=true
config.bHtml=true
if (objArgs)
for (var i in objArgs)
if (config[i] == 'undefined')
alert(i + ' is not a valid option')
else
config[i]=objArgs[i]
return config
}
Log.zpdumpObject=function(obj, name, configIn) {
var config=Log.zpdumpObject_config(configIn)
var self=this
var strEOL=config.bHtml ? "<br>" : '\n'
var strIndent=config.bHtml ? " " : '\t'
var funcRecurse=function(obj, name, depth, indent) {
var strName, strVal, strType, strOut
if (depth > config.maxdepth)
return indent + name + ": <Maximum Depth Reached>" + strEOL;
if (typeof obj == "object") {
var child = null;
var output = (indent + name)
if (config.bShowType)
output+=' (' + typeof obj + ')'
output+=strEOL
indent += strIndent
for (var item in obj) {
try {
child = obj[item];
} catch (e) {
child = "<Unable to Evaluate>";
}
strName=name+'.'+item
strType=typeof child
strOut = indent + strName
if (config.bShowType)
strOut += ' ('+strType+')'
if (strType=='object')
output += funcRecurse(child, strName, depth + 1, indent);
else
if (config.objSkip[strType] != true)
{
if (config.bShowValues)
{
if (config.bSkipEmpty && !child)
;
else
if (config.bSkipUndefined && strType == 'undefined')
;
else
output += strOut + "=" + child + strEOL;
}
else
output += strOut + strEOL;
}
}
return output;
}
else
{
return obj;
}
}
return funcRecurse(obj, name, 1, '')
}
var log = new Log(Log.DEBUG, Log.popupLogger);
Documentation generated by
JSDoc on Thu Aug 16 12:18:39 2007