usedjs
Version:
used.js 是一个轻量级小巧的可运行在浏览器中的 JavaScript 函数库
100 lines (99 loc) • 3.05 kB
JavaScript
;
/**
* 根据 https://developer.mozilla.org/zh-CN/docs/Web/API/Console 提供相应的等级日志
* 前四个类型属于消息输出,主要针对文本 String
* DIR 针对 DOM
* TABLE 针对 Object | Array
*/
Object.defineProperty(exports, "__esModule", { value: true });
var LoggerType;
(function (LoggerType) {
LoggerType["LOG"] = "log";
LoggerType["INFO"] = "info";
LoggerType["WARNING"] = "warning";
LoggerType["ERROR"] = "error";
LoggerType["DIR"] = "dir";
LoggerType["TABLE"] = "table";
})(LoggerType = exports.LoggerType || (exports.LoggerType = {}));
var fillZore = function (str) {
var res = '00' + str;
return res.substring(res.length - 2);
};
var printFunc = function (timeTag, type) {
return "time: " + timeTag + " | " + type + ": ";
};
var func = function (internal) {
var type = internal.type, message = internal.message, timeTag = internal.timeTag;
var str = printFunc(timeTag, type);
switch (type) {
case LoggerType.LOG: {
console.log.apply(console, [str].concat(message));
break;
}
case LoggerType.INFO: {
console.info.apply(console, [str].concat(message));
break;
}
case LoggerType.WARNING: {
console.warn.apply(console, [str].concat(message));
break;
}
case LoggerType.ERROR: {
console.error.apply(console, [str].concat(message));
break;
}
}
};
var funcDir = function (message) {
console.dir(message);
};
var funcTable = function (message, options) {
if (options) {
console.table(message, options);
}
else {
console.table(message);
}
};
var channel = function (message, type, options) {
if (type === void 0) { type = LoggerType.INFO; }
var time = new Date();
var timeTag = fillZore(time.getHours()) + ":" + fillZore(time.getMinutes()) + ":" + fillZore(time.getSeconds());
if (type === LoggerType.LOG || type === LoggerType.INFO || type === LoggerType.WARNING || type === LoggerType.ERROR) {
func({
type: type,
timeTag: timeTag,
message: message,
options: options,
});
}
else if (type === LoggerType.DIR) {
funcDir(message);
}
else if (type === LoggerType.TABLE) {
funcTable(message, options);
}
else {
console.error("暂时不支持这种类型的日志");
}
};
exports.dir = function (el) {
logger(el, LoggerType.DIR);
};
exports.info = function (message) {
logger(message, LoggerType.INFO);
};
exports.warning = function (message) {
logger(message, LoggerType.WARNING);
};
exports.error = function (message) {
logger(message, LoggerType.ERROR);
};
exports.table = function (message, options) {
logger(message, LoggerType.TABLE, options);
};
var logger = function (message, type, options) {
if (type === void 0) { type = LoggerType.LOG; }
channel(message, type, options);
};
exports.default = logger;