UNPKG

colog-node

Version:

## Project Description

176 lines (162 loc) 4.3 kB
const CologType = require("./CologType") /** * @func log工具类-彩色输出 * @returns */ class Colog { /** * @func 构造函数 */ constructor() {} // MARK - Public Functions // ************************************************************************ /** * @func verbose方法 * @param {String} msg: */ verbose(msg) { let disPlayMsg = this.getDisplayMsg(CologType.VERBOSE, msg) this.consolePrint(msg, disPlayMsg) } /** * @func DEBUG方法 * @param {String} msg: */ debug(msg) { let disPlayMsg = this.getDisplayMsg(CologType.DEBUG, msg) this.consolePrint(msg, disPlayMsg) } /** * @func INFO方法 * @param {String} msg: */ info(msg) { let disPlayMsg = this.getDisplayMsg(CologType.INFO, msg) this.consolePrint(msg, disPlayMsg) } /** * @func WARNING方法 * @param {String} msg: */ warning(msg) { let disPlayMsg = this.getDisplayMsg(CologType.WARNING, msg) this.consolePrint(msg, disPlayMsg) } /** * @func ERROR方法 * @param {String} msg: */ error(msg) { let disPlayMsg = this.getDisplayMsg(CologType.ERROR, msg) this.consolePrint(msg, disPlayMsg) } // MARK - Private Functions // ************************************************************************ /** * @func * @params * @returns */ consolePrint(msg, disPlayMsg) { console.log(disPlayMsg) if (typeof msg != "string") { console.log(msg) } } /** * @func 格式化msg * @params {CologType} type: 显示类型 * @params {String} msg: 显示msg * @returns */ getDisplayMsg(type, msg) { let titleBgColor = "" let titleFontColor = "" let msgBgColor = "" let msgFontColor = "" switch (type) { case CologType.VERBOSE: titleBgColor = "45" titleFontColor = "30" msgBgColor = "40" msgFontColor = "35" break case CologType.DEBUG: titleBgColor = "42" titleFontColor = "30" msgBgColor = "40" msgFontColor = "32" break case CologType.INFO: titleBgColor = "44" titleFontColor = "30" msgBgColor = "40" msgFontColor = "34" break case CologType.WARNING: titleBgColor = "43" titleFontColor = "30" msgBgColor = "40" msgFontColor = "33" break case CologType.ERROR: titleBgColor = "41" titleFontColor = "30" msgBgColor = "40" msgFontColor = "31" break default: break } let currentTime = this.getCurrentTime() let res = "[" + currentTime + "] \u001b[" + titleBgColor + ";" + titleFontColor + "m " + type + " \u001b[" + msgBgColor + ";" + msgFontColor + "m " + msg + " \u001b[0m" return res } /** * @func 获取当前时间 by format(YYYY-MM-DD HH:mm:ss) * @params * @returns {String}: 当前时间字符串 */ getCurrentTime() { var date = new Date() var month = date.getMonth() + 1 var strDate = date.getDate() var currentDate = date.getFullYear() + "-" + month.toString().padStart(2, "0") + "-" + strDate.toString().padStart(2, "0") + " " + date.getHours().toString().padStart(2, "0") + ":" + date.getMinutes().toString().padStart(2, "0") + ":" + date.getSeconds().toString().padStart(2, "0") return currentDate } } // 单例 let colog = new Colog() export { colog } // // module.exports = colog // module.exports = { // colog: colog // }