UNPKG

project-general-tools

Version:

项目开发通用工具类封装

184 lines (183 loc) 7.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /* * @Author: Mad Dragon 395548460@qq.com * @Date: 2020年3月21日 * @explanatory: 日志格式化输出 */ const lStorage_1 = __importDefault(require("./lStorage")); const moment_1 = __importDefault(require("./moment")); const localforage_1 = __importDefault(require("localforage")); const key = 'logStorage'; const maxLog = 39; // 设置 离线数据缓存 const setFStorageLog = (key, type, title, args) => { if (typeof window !== 'undefined') { const item = { time: moment_1.default.formatInitTime(new Date(), 'YYYY-MM-DD HH:mm:ss'), type, title, args, href: location.href }; /* eslint-disable */ const storage = localforage_1.default.createInstance({ name: key }); storage.keys().then((keys) => { // key 名 // console.log(keys) if (keys.length > maxLog) { let num = keys.length - maxLog; // console.log(keys.length, num); const newArr = keys.slice(0, num); newArr.forEach((item) => { // console.log(item) storage.removeItem(item).then(() => { // 当值被移除后,此处代码运行 // console.log(`Key is cleared! ==> ${item}`); }).catch((err) => { // 当出错时,此处代码运行 console.log(err); }); }); } }).catch((err) => { // 当出错时,此处代码运行 console.log(err); }); storage.setItem(moment_1.default.formatInitTime(new Date(), 'x'), JSON.stringify(item)); } if (typeof global !== 'undefined') { const item = { time: moment_1.default.formatInitTime(new Date(), 'YYYY-MM-DD HH:mm:ss'), type, title, args, href: title }; console.log(''); console.info(`***** ↓↓↓ [ NodeJS ][ info ] ${title} ↓↓↓ *****`); console.info(item); console.info(`***** ↑↑↑ [ NodeJS ][ info ] ${title} ↑↑↑ *****`); console.log(''); } }; // 设置 日志缓存 const setLog = (key, type, title, args) => { if (typeof window !== 'undefined') { let oldLogList = lStorage_1.default.getItem(key); const item = { time: moment_1.default.formatInitTime(new Date(), 'YYYY-MM-DD HH:mm:ss'), type, title, args, href: location.href }; if (oldLogList) { oldLogList = JSON.parse(oldLogList); if (oldLogList.length > 39) { oldLogList.pop(); } oldLogList.unshift(item); lStorage_1.default.setItem(key, JSON.stringify(oldLogList)); } else { lStorage_1.default.setItem(key, JSON.stringify([item])); } } if (typeof global !== 'undefined') { const item = { time: moment_1.default.formatInitTime(new Date(), 'YYYY-MM-DD HH:mm:ss'), type, title, args, href: title }; console.log(''); console.info(`***** ↓↓↓ [ NodeJS ][ info ] ${title} ↓↓↓ *****`); console.info(item); console.info(`***** ↑↑↑ [ NodeJS ][ info ] ${title} ↑↑↑ *****`); console.log(''); } }; exports.default = { setLog: setLog, setFStorageLog: setFStorageLog, info(title, ...args) { setFStorageLog(key, 'info', title, args); console.log(''); console.info(`***** ↓↓↓ [ Windows ][ info ] ${title} ↓↓↓ *****`); console.info(args); console.info(`***** ↑↑↑ [ Windows ][ info ] ${title} ↑↑↑ *****`); console.log(''); }, error(title, ...args) { setFStorageLog(key, 'error', title, args); console.log(''); console.error(`***** ↓↓↓ [ Windows ][ error ] ${title} ↓↓↓ *****`); console.error(args); console.error(`***** ↑↑↑ [ Windows ][ error ] ${title} ↑↑↑ *****`); console.log(''); }, debug(title, ...args) { setFStorageLog(key, 'debug', title, args); console.log(''); console.debug(`***** ↓↓↓ [ Windows ][ debug ] ${title} ↓↓↓ *****`); console.debug(args); console.debug(`***** ↑↑↑ [ Windows ][ debug ] ${title} ↑↑↑ *****`); console.log(''); }, warn(title, ...args) { setFStorageLog(key, 'warn', title, args); console.log(''); console.warn(`***** ↓↓↓ [ Windows ][ warn ] ${title} ↓↓↓ *****`); console.warn(args); console.warn(`***** ↑↑↑ [ Windows ][ warn ] ${title} ↑↑↑ *****`); console.log(''); }, table(title, ...args) { setFStorageLog(key, 'table', title, args); console.log(''); console.log(`***** ↓↓↓ [ Windows ][ table ] ${title} ↓↓↓ *****`); console.table(...args); console.log(`***** ↑↑↑ [ Windows ][ table ] ${title} ↑↑↑ *****`); console.log(''); } }; /** * _ooOoo_ * o8888888o * 88" . "88 * (| -_- |) * O\ = /O * ____/`---'\____ * .' \\| |// `. * / \\||| : |||// \ * / _||||| -:- |||||- \ * | | \\\ - /// | | * | \_| ''\---/'' | | * \ .-\__ `-` ___/-. / * ___`. .' /--.--\ `. . __ * ."" '< `.___\_<|>_/___.' >'"". * | | : `- \`.;`\ _ /`;.`/ - ` : | | * \ \ `-. \_ __\ /__ _/ .-` / / * ======`-.____`-.___\_____/___.-`____.-'====== * `=---=' * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * 佛祖保佑 永无BUG * 佛曰: * 写字楼里写字间,写字间里程序员; * 程序人员写程序,又拿程序换酒钱。 * 酒醒只在网上坐,酒醉还来网下眠; * 酒醉酒醒日复日,网上网下年复年。 * 但愿老死电脑间,不愿鞠躬老板前; * 奔驰宝马贵者趣,公交自行程序员。 * 别人笑我忒疯癫,我笑自己命太贱; * 不见满街漂亮妹,哪个归得程序员? * * Mad Dragon 395548460@qq.com */