NAVIGATION
Home
Gallery
Java
Linux
Web
Scripts And Utilities
Mobile And Sms
Misc
Contact
pixelWIKI
Nabaz Tag




<<

p72log Util

This script allows you to easily add logging to javascript. Litter your code with calls to p72logUtil.debug() (or .info() .warn() etc...), set a log level, and matching events will show in a popup window. .fatal() events will also trigger a javascript alert. Include the script on your page. Closing the popup window will clear the logs.

Methods
p72logUtil.init( _level )
Used to initialise the logger with a given log level. Valid log levels are OFF, FATAL, ERROR, WARN, INFO, DEBUG and TRACE. This method can also be used to dynamically change the log level.
NOTE init() must be called AFTER the p72logUtil code.

p72logUtil.fatal( _msg[, _e] )
Create a FATAL level event with the given _msg. Optionally, pass an exception _e to include exception information (description and line number).
FATAL messages also trigger a javascript alert

p72logUtil.error( _msg[, _e] )
As p72logUtil.fatal() but without the javascript alert

p72logUtil.warn( _msg )
Create a WARN level event with the given _msg.

p72logUtil.info( _msg )
Create a WARN level event with the given _msg.

p72logUtil.debug( _msg )
Create a WARN level event with the given _msg.

p72logUtil.trace( _msg )
Create a WARN level event with the given _msg.

Example

1:
2:
3:
4:
5:
6:
7:
8:
9:
p72logUtil.init( p72logUtil.INFO );
p72logUtil.debug( "this will not be shown" );
p72logUtil.info( "this will be shown as INFO" );
try {
   do_something_wrong;
} catch (e) {
   p72logUtil.error( "this will be shown as ERROR", e );
   p72logUtil.fatal( "this will be shown as FATAL and popup an alert", e );
}


The Code

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
/**
* pixelSEVENTY2 javascript logger
* 
* Author: Chris Hembrow
*   Date: 2007-09-25
*
* The latest version of this script can always be found at http://www.pixelSEVENTY2.net/pixel/p72formChecker.page
*
*/

var p72logUtil = {

	  OFF : 0
	, FATAL : 1
	, ERROR : 2
	, WARN : 3
	, INFO : 4
	, DEBUG : 5
	, TRACE : 6

	, _LEVEL : 0
	
	, init : function( _level ) {
		this._LEVEL = _level
	}
	
	, fatal : function( _msg, _e ) {
		if ( this._LEVEL >= this.FATAL ) {
			if ( _e != undefined ) {
				line = ( _e.lineNumber != undefined ) ? "nline: " + _e.lineNumber : "";
				this._write( "FATAL", _msg + "n" + _e.message + line );
				alert( "ERRORn" + _msg + "n" + _e.message + line );
			} else {
				if ( _e.message != undefined ) {
					line = ( _e.lineNumber != undefined ) ? "nline: " + _e.lineNumber : "";
					this._write( "FATAL", _msg.message + line );
					alert( "ERRORn" + _msg.message + line );
				} else {
					this._write( "FATAL", _msg );
					alert( "ERRORn" + _msg );
				}						
			}
		}
	}
	
	, error : function( _msg, _e ) {
		if ( this._LEVEL >= this.ERROR ) {
			if ( _e != undefined ) {
				line = ( _e.lineNumber != undefined ) ? "nline: " + _e.lineNumber : "";
				this._write( "ERROR", _msg + "n" + _e.message + line );
			} else {
				if ( _e.message != undefined ) {
					line = ( _e.lineNumber != undefined ) ? "nline: " + _e.lineNumber : "";
					this._write( "ERROR", _msg.message + line );
				} else {
					this._write( "ERROR", _msg );
				}						
			}
		}
	}
	
	, warn : function( _msg ) {
		this._log( this.WARN, "WARN", _msg );
	}
	
	, info : function( _msg ) {
		this._log( this.INFO, "INFO", _msg );
	}
	
	, debug : function( _msg ) {
		this._log( this.DEBUG, "DEBUG", _msg );
	}
	
	, trace : function( _msg ) {
		this._log( this.TRACE, "TRACE", _msg );
	}
	
	, _log : function( _level, _levelString, _msg ) {
		if ( this._LEVEL >= _level ) {
			this._write( _levelString, _msg );
		}
	}
	
	, _debugWindow : null
	, _logUnload : null
	, _lineCount : 0
	, _write : function( _level, _msg ) {
		try {
			if ( this._debugWindow == null ) {	
				this._debugWindow = window.open( "", "DEBUG", "width=500,height=" + ( screen.availHeight - 40 ) + ",scrollbars,scrolling,resizable" );
				if ( ! this._debugWindow ) {
					return;
				}
				this._debugWindow.moveTo(20, 20);
				this._debugWindow.document.write("<html><head><title>DEBUGGING " + self.location.href + "</title><style type="text/css">body { margin: 0px; padding: 0px; } pre { margin: 0px; padding: 3px; border-bottom: 1px solid #BBB; white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word; } </style></head><body></body></html>");
				this._debugWindow.document.close();
				if ( document.all ) {
					this._debugWindow.document.body.onunload = function() { try { p72logUtil._debugWindow = null; } catch (e) {} }
				} else {
					this._debugWindow.onunload = function() { try { p72logUtil._debugWindow = null; } catch (e) {} }
				}
				if ( self.onunload != undefined ) {
					this._logUnload = self.onunload;
				}
				self.onunload = function() { 
					if ( p72logUtil._logUnload != null ) {
						try {
							p72logUtil._logUnload();
						} catch (e) {}
					}
					if ( p72logUtil._debugWindow != null ) {
						p72logUtil._debugWindow.close();
					}
				}
				self.focus();
			}
			pre = this._debugWindow.document.createElement( "pre" );
			this._lineCount++;
			line = this._lineCount;
			for ( _lc=0; _lc<3; _lc++ ) {
				if ( this._lineCount < ( 10 * ( _lc+1 ) ) ) { line = " " + line; }
			}
			pre.appendChild( this._debugWindow.document.createTextNode( line + ": " + new Date().toLocaleString() + " [" + _level + "] " + _msg + "rn" ) );
			pre.style.backgroundColor = this._lineCount % 2 == 0 ? "#FFF" : "#DDD";
			this._debugWindow.document.body.appendChild( pre );
		} catch (e) { alert( "ERROR: LOGGING NOT WORKING" ); }
	}

};