/* Copyright (c) 2004-2006, The Dojo Foundation All Rights Reserved. Licensed under the Academic Free License version 2.1 or above OR the modified BSD license. For more information on Dojo licensing, see: http://dojotoolkit.org/community/licensing.shtml */ /* This is the dojo logging facility, which is imported from nWidgets (written by Alex Russell, CLA on file), which is patterned on the Python logging module, which in turn has been heavily influenced by log4j (execpt with some more pythonic choices, which we adopt as well). While the dojo logging facilities do provide a set of familiar interfaces, many of the details are changed to reflect the constraints of the browser environment. Mainly, file and syslog-style logging facilites are not provided, with HTTP POST and GET requests being the only ways of getting data from the browser back to a server. Minimal support for this (and XML serialization of logs) is provided, but may not be of practical use in a deployment environment. The Dojo logging classes are agnostic of any environment, and while default loggers are provided for browser-based interpreter environments, this file and the classes it define are explicitly designed to be portable to command-line interpreters and other ECMA-262v3 envrionments. the logger needs to accomidate: log "levels" type identifiers file? message tic/toc? The logger should ALWAYS record: time/date logged message type level */ // TODO: conver documentation to javadoc style once we confirm that is our choice // TODO: define DTD for XML-formatted log messages // TODO: write XML Formatter class // TODO: write HTTP Handler which uses POST to send log lines/sections // Filename: LogCore.js // Purpose: a common logging infrastructure for dojo // Classes: dojo.logging, dojo.logging.Logger, dojo.logging.Record, dojo.logging.LogFilter // Global Objects: dojo.logging // Dependencies: none dojo.provide("dojo.logging.Logger"); dojo.provide("dojo.log"); dojo.require("dojo.lang"); /* A simple data structure class that stores information for and about a logged event. Objects of this type are created automatically when an event is logged and are the internal format in which information about log events is kept. */ dojo.logging.Record = function(lvl, msg){ this.level = lvl; this.message = msg; this.time = new Date(); // FIXME: what other information can we receive/discover here? } // an empty parent (abstract) class which concrete filters should inherit from. dojo.logging.LogFilter = function(loggerChain){ this.passChain = loggerChain || ""; this.filter = function(record){ // FIXME: need to figure out a way to enforce the loggerChain // restriction return true; // pass all records } } dojo.logging.Logger = function(){ this.cutOffLevel = 0; this.propagate = true; this.parent = null; // storage for dojo.logging.Record objects seen and accepted by this logger this.data = []; this.filters = []; this.handlers = []; } dojo.lang.extend(dojo.logging.Logger, { argsToArr: function(args){ // utility function, reproduced from __util__ here to remove dependency var ret = []; for(var x=0; x= this.cutOffLevel; }, getEffectiveLevel: function(){ if((this.cutOffLevel==0)&&(this.parent)){ return this.parent.getEffectiveLevel(); } return this.cutOffLevel; }, addFilter: function(flt){ this.filters.push(flt); return this.filters.length-1; }, removeFilterByIndex: function(fltIndex){ if(this.filters[fltIndex]){ delete this.filters[fltIndex]; return true; } return false; }, removeFilter: function(fltRef){ for(var x=0; x=this.cutOffLevel)){ this.parent.log(lvl, msg); return false; } // FIXME: need to call logging providers here! this.handle(new dojo.logging.Record(lvl, msg)); return true; }, // logger helpers debug:function(msg){ return this.logType("DEBUG", this.argsToArr(arguments)); }, info: function(msg){ return this.logType("INFO", this.argsToArr(arguments)); }, warning: function(msg){ return this.logType("WARNING", this.argsToArr(arguments)); }, error: function(msg){ return this.logType("ERROR", this.argsToArr(arguments)); }, critical: function(msg){ return this.logType("CRITICAL", this.argsToArr(arguments)); }, exception: function(msg, e, squelch){ // FIXME: this needs to be modified to put the exception in the msg // if we're on Moz, we can get the following from the exception object: // lineNumber // message // fileName // stack // name // on IE, we get: // name // message (from MDA?) // number // description (same as message!) if(e){ var eparts = [e.name, (e.description||e.message)]; if(e.fileName){ eparts.push(e.fileName); eparts.push("line "+e.lineNumber); // eparts.push(e.stack); } msg += " "+eparts.join(" : "); } this.logType("ERROR", msg); if(!squelch){ throw e; } }, logType: function(type, args){ var na = [dojo.logging.log.getLevel(type)]; if(typeof args == "array"){ na = na.concat(args); }else if((typeof args == "object")&&(args["length"])){ na = na.concat(this.argsToArr(args)); /* for(var x=0; x=this.cutOffLevel)){ this.emit(record); } } dojo.logging.LogHandler.prototype.emit = function(record){ // do whatever is necessaray to actually log the record dojo.unimplemented("emit"); } // set aliases since we don't want to inherit from dojo.logging.Logger void(function(){ // begin globals protection closure var names = [ "setLevel", "addFilter", "removeFilterByIndex", "removeFilter", "removeAllFilters", "filter" ]; var tgt = dojo.logging.LogHandler.prototype; var src = dojo.logging.Logger.prototype; for(var x=0; xthis.numRecords){ this.data.shift(); } } } dojo.logging.logQueueHandler = new dojo.logging.MemoryLogHandler(0,50,0,10000); // actual logging event handler dojo.logging.logQueueHandler.emit = function(record){ // we should probably abstract this in the future var logStr = String(dojo.log.getLevelName(record.level)+": "+record.time.toLocaleTimeString())+": "+record.message; if(!dj_undef("debug", dj_global)){ dojo.debug(logStr); }else if((typeof dj_global["print"] == "function")&&(!dojo.render.html.capable)){ print(logStr); } this.data.push(record); if(this.numRecords != -1){ while(this.data.length>this.numRecords){ this.data.shift(); } } } dojo.logging.log.addHandler(dojo.logging.logQueueHandler); dojo.log = dojo.logging.log;