wxbm_logger.js
const machine = "real";
const loggerlevel = "INFO";
function getLogger(inamespace) {
const level = loggerlevel.toUpperCase();
const namespace = inamespace == undefined || inamespace == null ? '' : inamespace;
this.error = function() {
if (level == "ERROR" || level == "DEBUG") {
consoleTemplate("green", "red", "error", namespace,machine, arguments);
}
}
this.warn = function() {
if (level == "WARN" || level == "DEBUG") {
consoleTemplate("green", "yellow", "warn", namespace, machine, arguments);
}
}
this.info = function() {
if (level == "INFO" || level == "DEBUG") {
consoleTemplate("green", "#4a88ee", "info", namespace, machine, arguments);
}
}
this.debug = function() {
if (level == "DEBUG") {
consoleTemplate("green", "grey", "debug", namespace, machine, arguments);
}
}
}
function consoleTemplate() {
console.log(
"[%c%s%c] Line%i %c%s%c %s:", "color:" + arguments[0],
loggerformatTime(new Date()), "color:black", arguments[5].length, "color:" + arguments[1],
arguments[2], "color:black", arguments[3]
);
for (let li = 0; li < arguments[5].length; li++) {
consolePrint((li + 1), arguments[5][li]);
}
if (arguments[4].toUpperCase() =="SIMULATOR"){
console.groupCollapsed("追踪函数的调用轨迹:");
console.trace();
console.groupEnd();
}
}
function consolePrint(li, obj) {
const type = typeof obj;
switch (type) {
case "number":
console.log("L" + li + " --->%s", obj);
break;
case "string":
console.log("L" + li + " --->%s", obj);
break;
case "boolean":
console.log("L" + li + " --->%s", obj);
break;
case "object":
if (Object.prototype.toString.call(obj) == '[object Array]') {
console.log("L" + li + " --->[" + obj[0] + "]:%o", obj[1]);
} else {
console.log("L" + li + " --->%o", obj);
}
break;
case "function":
console.log("L" + li + " --->%s", "function");
break;
case "undefined":
console.log("L" + li + " --->%s", "undefined");
break;
default:
console.log("L" + li + " --->%s", "default");
}
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
const loggerformatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
const millisecond = date.getMilliseconds()
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second, millisecond].map(formatNumber).join(':')
}
module.exports = {
GetLogger: getLogger
}
引入方式
import LOGGER from './wxbm_logger.js';
const logger = new LOGGER.GetLogger('utils/wxbm_http');
使用方式
logger.error();
logger.warn();
logger.info();
logger.debug();
可以有的参数:
1,2,3,... ...
打印 line1 1 line2 2 line3 3 ... ...
["名称":"小白蛇"]
打印 line1 ["名称"]:"小白蛇"
以上补充:不局限于数字和字符串,object类型都行,最终都是console.log打印
好文章