📄 logger.js
字号:
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* 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.
*/
/**
* Framework logging classes
*/
var DEBUG_LEVEL=1;
var INFO_LEVEL=2;
var WARNING_LEVEL=3;
var ERROR_LEVEL=4;
var DEBUGALL_LEVEL=5;
var VERBOSE_LEVEL = INFO_LEVEL;
var DEBUGGING = (VERBOSE_LEVEL <= DEBUG_LEVEL);
function EmptyLogger() {
if(!Cfx.Class.IsDefined(EmptyLogger)) {
Cfx.Class.New(EmptyLogger);
if(Cfx.Class.IsInitializing(EmptyLogger)) {
EmptyLogger.Method(debug);
EmptyLogger.Method(info);
EmptyLogger.Method(warn);
EmptyLogger.Method(error);
EmptyLogger.Method(getChildLogger);
EmptyLogger.Method(debugging);
return;
}
}
// Setup instance data.
this.InitInstance();
if(arguments.length) {
this.category = arguments[0];
}
return this;
function debug(){}
function info(){}
function warn(){}
function error(ex, message){
var theMessage = '';
if(!Cfx.Js.IsEmpty(ex)) {
if(!Cfx.Js.IsEmpty(ex.name) && !Cfx.Js.IsEmpty(ex.message)) {
theMessage = " "+ ex.name + " " +ex.message;
if(!Cfx.Js.IsEmpty(ex.line))
theMessage += " line: " + ex.line;
}else {
theMessage = ex;
}
}
var logMsg = new LogMessage(theMessage, this.category, "log_error");
var errorMessage = logMsg.value;
if (getErrorMessage) {
errorMessage = getErrorMessage(logMsg.value);
}
if (errorMessage!=null) {
alert(errorMessage);
}
}
function getChildLogger(_category){
return new EmptyLogger(this.category+"."+_category);
}
function debugging(){
return false;
}
}
/**
* Default logger object.
* Implements a set of functions that respond to log messages.
*
*/
function DefaultLogger() {
if(!Cfx.Class.IsDefined(DefaultLogger)) {
Cfx.Class.New(DefaultLogger);
if(Cfx.Class.IsInitializing(DefaultLogger)) {
DefaultLogger.Method(debug);
DefaultLogger.Method(info);
DefaultLogger.Method(warn);
DefaultLogger.Method(error);
DefaultLogger.Method(getChildLogger);
DefaultLogger.Method(debugging);
DefaultLogger.Method(printMessage);
DefaultLogger.Method(getTimeString);
DefaultLogger.Method(createTimePrefix);
return;
}
}
// Setup instance data.
this.InitInstance();
if(arguments.length) {
this.category = arguments[0]; //identifier in the list of messages of a logger
this.outputFrame = arguments[1]; //frame containing the logger
this.logger = arguments[2]; //identifier of logger
this.level = arguments[3]; //log level
}
// Return instance.
return this;
function debug(message) {
if(this.debugging()) {
if(message == null) {
message = "Empty message in log object";
}
var logMsg = new LogMessage(message, this.createTimePrefix("DEBUG", this.category), "log_debug");
this.printMessage(logMsg);
}
}
function info( message ) {
if(this.level > INFO_LEVEL) {
return;
}
if(message == null) {
message = "Empty message in log object";
}
var logMsg = new LogMessage(message, this.createTimePrefix("INFO", this.category), "log_info");
this.printMessage(logMsg);
}
function warn( message ) {
if(this.level > INFO_LEVEL) {
return;
}
if (message == null) {
message = "Empty message in log object";
}
var logMsg = new LogMessage(message, this.createTimePrefix("WARNING", this.category), "log_warning");
this.printMessage(logMsg);
}
function error( ex, message ) {
var logMsg = new LogMessage(message, this.createTimePrefix("ERROR", this.category), "log_error");
if (ex != null) {
if (ex.name != null && ex.message != null) {
logMsg.value += " "+ ex.name + " " +ex.message;
} else {
logMsg.value += " "+ex;
}
if (ex.line != null)
logMsg.value += " line: " + ex.line;
}
this.printMessage(logMsg);
}
function getChildLogger(childCategory, logLevel) {
var _level = logLevel;
if(_level==null)
_level = this.level;
if(this.level == DEBUGALL_LEVEL)
return new DefaultLogger(this.category + "." + childCategory , this.outputFrame, this.logger, this.level);
else
return new DefaultLogger(this.category + "." + childCategory , this.outputFrame, this.logger, _level);
}
function printMessage(logMsg) {
try {
this.outputFrame.addMessage(this.logger, logMsg);
} catch (e) {
alert("Can't print into log window " + e.text + ' - ' + e.message + '\n' + this.logger + ": " + logMsg);
}
}
function getTimeString() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var milliseconds = now.getMilliseconds();
var result = "";
if (hours <10)
result +="0";
result += hours + ":";
if (minutes <10)
result +="0";
result += minutes + ":";
if (seconds <10)
result +="0";
result += seconds + ":";
if (milliseconds <10)
result +="0";
if (milliseconds <100)
result +="0";
if (milliseconds <1000)
result +="0";
result += milliseconds;
return result;
}
function createTimePrefix(levelName,category) {
var prefix = levelName + " " + this.getTimeString();
if (category != null)
prefix = prefix + " [" + category + "]";
prefix = prefix + " ";
return prefix;
}
function debugging() {
return this.level==DEBUG_LEVEL || this.level==DEBUGALL_LEVEL;
}
}
/**
* Wrapper object for a log message.
* Has an toHTml method for printing itself.
*/
function LogMessage() {
if(!Cfx.Class.IsDefined(LogMessage)) {
Cfx.Class.New(LogMessage);
if(Cfx.Class.IsInitializing(LogMessage)) {
LogMessage.Method(toHtml);
return;
}
}
// Setup instance data.
this.InitInstance();
if(arguments.length) {
this.value = arguments[0];
this.prefix = arguments[1];
this.style = arguments[2];
}
// Return instance.
return this;
function toHtml() {
//simple
return '<div class="' + this.style + '">' + this.prefix + " " + this.value + '</div>';
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -