1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | dojo.provide("html5.Log"); dojo.require("dojo.date.locale"); dojo.declare("html5.Log", null, { SPACE:4, MSGMAP:{"1":"ERROR","2":"WARNING","3":"INFO","4":"DEBUG"}, // 映射 log 级别到 console 中的方法名称 METHODMAP:{"1":"error","2":"warn","3":"info","4":"debug"}, constructor: function() { //1 表示记录错误信息,2 表示记录警告信息,3 表示记录一般信息,4 表示记录调式信息 this.logLevel = 1; this.indent = 0; }, getLogLevel: function() { return this.logLevel; }, setLogLevel: function(logLevel) { this.logLevel = logLevel; }, isDebugEnabled: function() { return this.logLevel >= 4; } }); |
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 | dojo.declare("html5.Log", null, { // 记录错误信息 error: function(location, message, extra) { this._record(1, location, message, extra); }, // 省略 info,warn,debug 方法,方法参数和方法主体和 error 方法类似。 // 进入一个函数 entry: function(location, extra) { this._record(-1, location, "Entry", extra); this.indent += this.SPACE; } // 省略 exit 方法,具体实现参考附件中的源代码。 _record: function(level, location, message, extra) { var logEntry = { level: level, time: new Date(), loc: location, msg: message, extra: extra, indent: this.indent }; if (level <= this.logLevel) { var logInfo = this._formatLogEntry(logEntry); // 根据 log 级别映射到 console 中的不同方法,把信息输出到浏览器控制台 if(this.METHODMAP[level+""]){ console[this.METHODMAP[level+""]](logInfo); } else{ console.log(logInfo); } } } }); |
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 | dojo.declare("html5.Log", null, { _formatLogEntry: function(logEntry) { var result = dojo.date.locale.format(logEntry.time, {datePattern: "yyyy-MM-dd", timePattern: "HH:mm:ss "}); for ( var i = 0; i < logEntry.indent; i++) { result += " "; } if(logEntry.level>=0){ result += this.MSGMAP[logEntry.level+""]+ " - "; }// 在每条记录前加入 log 的时间已经 log 的级别 result += logEntry.loc; if (logEntry.msg) { result += " - " + this._formatObject(logEntry.msg); } if (logEntry.extra) { result += " - " + this._formatObject(logEntry.extra); } result += "\r\n"; return result; }, // 格式化 Object 类型信息 _formatObject: function(data) { if (dojo.isObject(data)) { return dojo.toJson(data); } else { return data; } } }); |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |