UNPKG

@dexare/logger

Version:

A Dexare module that provides colorful logging

164 lines (163 loc) 6.44 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const dexare_1 = require("dexare"); const winston_1 = __importStar(require("winston")); const dayjs_1 = __importDefault(require("dayjs")); const chalk_1 = __importDefault(require("chalk")); const util = __importStar(require("util")); const colorPool = [ chalk_1.default.black.bgCyan, chalk_1.default.black.bgYellow, chalk_1.default.black.bgRed, chalk_1.default.black.bgGreen, chalk_1.default.black.bgBlue, chalk_1.default.black.bgMagenta, chalk_1.default.black.bgGrey, chalk_1.default.black.bgCyanBright, chalk_1.default.black.bgYellowBright, chalk_1.default.black.bgRedBright, chalk_1.default.black.bgGreenBright, chalk_1.default.black.bgBlueBright, chalk_1.default.black.bgMagentaBright, chalk_1.default.cyan.bgBlack, chalk_1.default.yellow.bgBlack, chalk_1.default.red.bgBlack, chalk_1.default.green.bgBlack, chalk_1.default.blue.bgBlack, chalk_1.default.magenta.bgBlack, chalk_1.default.grey.bgBlack, chalk_1.default.cyanBright.bgBlack, chalk_1.default.yellowBright.bgBlack, chalk_1.default.redBright.bgBlack, chalk_1.default.greenBright.bgBlack, chalk_1.default.blueBright.bgBlack, chalk_1.default.magentaBright.bgBlack ]; class LoggerModule extends dexare_1.DexareModule { constructor(client) { super(client, { name: 'logger', description: 'Colorful logging with the winston module' }); this.moduleColors = { dexare: chalk_1.default.black.bgRed, eris: chalk_1.default.black.bgCyan, commands: chalk_1.default.black.bgYellow, sys: chalk_1.default.black.bgGray, dbots: chalk_1.default.black.bgYellowBright }; this.levelColors = { info: chalk_1.default.black.bgCyan, warn: chalk_1.default.black.bgYellow, error: chalk_1.default.black.bgRed, debug: chalk_1.default.magenta.bgBlack }; this.client.logErisEvents(); this.filePath = __filename; } load() { this.registerEvent('logger', this.onLog.bind(this)); } unload() { this.unregisterAllEvents(); } get config() { return this.client.config.logger; } async onLog(_, level, moduleName, args, extra) { if (!winston_1.default.loggers.has(moduleName)) winston_1.default.loggers.add(moduleName, { format: winston_1.format.combine(winston_1.format.printf((info) => { const lClk = this.levelColors[info.level] || chalk_1.default.yellow.bgBlack; const mClk = this.moduleColors[moduleName] || colorPool[Math.abs(this._hashCode(moduleName)) % colorPool.length]; return (mClk(` ${moduleName} `) + chalk_1.default.black.bgWhite(` ${(0, dayjs_1.default)().format('MM/DD HH:mm:ss')} `) + lClk(this._centrePad(info.level, 10)) + (info.id !== undefined ? chalk_1.default.black.bgYellowBright(` ${info.id} `) : '') + ` ${info.message}`); })), transports: [ new winston_1.default.transports.Console({ level: this.config?.level || (process.env.NODE_ENV === 'production' ? 'info' : 'debug') }) ] }); const text = []; // Util formatting if (typeof args[0] === 'string') { const formats = args[0].match(/%[sdifjoO]/g); if (formats) { const a = args.splice(1, formats.length); text.push(util.format(args.shift(), ...a)); } else text.push(chalk_1.default.white(args.shift())); } // Colorize the rest of the arguments for (const arg of args) { if (typeof arg === 'string') { text.push(chalk_1.default.magenta(`'${arg}'`)); } else if (typeof arg === 'number') { text.push(chalk_1.default.cyan(arg.toString())); } else if (typeof arg === 'object') { text.push('\n'); if (arg instanceof Error) { text.push(chalk_1.default.red(arg.stack)); } else { text.push(util.inspect(arg, this.config?.inspectOptions || {})); } } else text.push(arg); } winston_1.default.loggers.get(moduleName).log(level, text.join(' '), extra); } _centrePad(text, length) { if (text.length < length) return (' '.repeat(Math.floor((length - text.length) / 2)) + text + ' '.repeat(Math.ceil((length - text.length) / 2))); else return text; } _hashCode(str) { var hash = 0, i, chr; for (i = 0; i < str.length; i++) { chr = str.charCodeAt(i); hash = (hash << 5) - hash + chr; hash |= 0; } return hash; } } exports.default = LoggerModule;