ptool
Version:
vue项目开发通用工具类封装
112 lines (111 loc) • 3.68 kB
JavaScript
;
/**
* @FileName log.ts
* @Author Mad Dragon <395548460@qq.com>
* @Version V 0.0.1
* @Date 2021/4/13 11:53
* @Title
* @Desc
**/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const moment_1 = __importDefault(require("./moment"));
const lStorage_1 = __importDefault(require("./lStorage"));
const localforage_1 = __importDefault(require("localforage"));
const maxLog = 50;
const setErrorLog = (key, info) => {
if (typeof window !== 'undefined') {
lStorage_1.default.setItem(key, info);
}
else {
console.log('[ ERROR ]', info);
}
};
const setApiLog = (key, info) => {
if (typeof window !== 'undefined') {
// @ts-ignore
info.time = moment_1.default.format();
const storage = localforage_1.default.createInstance({ name: key });
storage.keys().then((keys) => {
if (keys.length > maxLog) {
const num = keys.length - maxLog;
const newArr = keys.slice(0, num);
newArr.forEach((item) => {
storage.removeItem(item).then(() => {
// 当值被移除后,此处代码运行
}).catch((err) => {
console.log(err);
});
});
}
}).catch((err) => {
console.error(err);
});
// @ts-ignore
if (info.status !== 200) {
setErrorLog('errorMSG', info);
}
storage.setItem(moment_1.default.format(new Date(), 'x'), JSON.stringify(info));
}
else {
// @ts-ignore
info.time = moment_1.default.format();
console.log('[ API ]', info);
}
};
const setConsoleLog = (key, info) => {
if (typeof window !== 'undefined') {
// @ts-ignore
info.time = moment_1.default.format();
const storage = localforage_1.default.createInstance({ name: key });
storage.keys().then((keys) => {
if (keys.length > maxLog) {
const num = keys.length - maxLog;
const newArr = keys.slice(0, num);
newArr.forEach((item) => {
storage.removeItem(item).then(() => {
// 当值被移除后,此处代码运行
}).catch((err) => {
console.log(err);
});
});
}
}).catch((err) => {
console.error(err);
});
storage.setItem(moment_1.default.format(new Date(), 'x'), JSON.stringify(info));
}
else {
// @ts-ignore
info.time = moment_1.default.format();
console.log('[ API ]', info);
}
};
const consoleMSG = (type, title, args) => {
if (type === 'table') {
// @ts-ignore
console.log(`[ ${type} ] => ${title}`);
console.table([...args]);
}
// @ts-ignore
console[type](`[ ${type} ] => ${title}`, args);
const consoleInfo = {
title: title,
type,
url: location.pathname + location.search,
data: args
};
setConsoleLog('logStorage', consoleInfo);
};
exports.default = {
setConsoleLog,
setErrorLog,
setApiLog,
info: (title, args) => consoleMSG('info', title, args),
error: (title, args) => consoleMSG('error', title, args),
debug: (title, args) => consoleMSG('debug', title, args),
warn: (title, args) => consoleMSG('warn', title, args),
table: (title, args) => consoleMSG('table', title, args)
};