From 31d5741707349aab0d52dc87ce8bf6bac4f1abee Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Mon, 24 Oct 2022 13:21:04 -0600 Subject: [PATCH] custom log types --- bot.js | 10 +++++++--- src/util/log/log.js | 32 +++++++++++++++++++------------- src/util/log/types.js | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/bot.js b/bot.js index 8ecaf7b..b7dc006 100644 --- a/bot.js +++ b/bot.js @@ -1,14 +1,18 @@ const logger = require('./src/util/log/log'); const client = {test: 'e', config: {logLevel: 1}}; -const log = logger(client); +const loggers = logger(client); +const log = loggers.log; log(); log("Hello World!") log("Hello in blue", {color: 'blue'}); log('A strange warning appears!', {color: 'yellowBright', source: 'warn', sourceColor: 'yellow'}); -log('Extra line spacing before', {}, false, true); +/*log('Extra line spacing before', {}, false, true); log('regular line'); log('extra line spacing after', {}, true); log('regular line'); log('both line spaces', {}, true, true); log('regular line'); -log('a custom fancy pink color', {color: '#ff00ff', sourceColor: '#660066'}); \ No newline at end of file +log('a custom fancy pink color', {color: '#ff00ff', sourceColor: '#660066'});*/ +loggers.warn("A warning appears!"); +loggers.error("Something bad happened!"); +loggers.success("Something good happened!"); \ No newline at end of file diff --git a/src/util/log/log.js b/src/util/log/log.js index dc13f7b..ebdc271 100644 --- a/src/util/log/log.js +++ b/src/util/log/log.js @@ -1,6 +1,7 @@ const chalk = require('chalk'); const getLevel = require('./getlevel'); +const types = require('./types'); const config = require('../../json/config.json'); @@ -12,19 +13,24 @@ let defaultOptions = { suffix: " >> " }; +const tlog = (client) => (message = "Test Log", options = {}, newLine = false, spacer = false) => { + let opt = {}; + opt.color = options.color || defaultOptions.color; + opt.level = ['string', 'number'].includes(typeof options.level) ? options.level : defaultOptions.level; + opt.suffix = typeof options.suffix === 'string' ? options.suffix : defaultOptions.suffix; + opt.source = options.source || defaultOptions.source; + opt.sourceColor = options.sourceColor || defaultOptions.sourceColor; + try {if (client.config.logLevel < opt.level) {return;}} + catch { + client.config.logLevel = getLevel(client.config.logLevel); + if (client.config.logLevel < opt.level) {return;} + } + console.log(`${spacer ? '\n' : ''}${(opt.sourceColor.startsWith('#') ? chalk.hex(opt.sourceColor) : chalk[opt.sourceColor])(`[${opt.source.toUpperCase()}]`)}${opt.suffix}${options.nc || options.noColor ? message : (opt.color.startsWith('#') ? chalk.hex(opt.color) : chalk[opt.color])(message)}${newLine ? '\n' : ''}`); +}; + module.exports = (client) => { - return (message = "Test Log", options = {}, newLine = false, spacer = false) => { - let opt = {}; - opt.color = options.color || defaultOptions.color; - opt.level = ['string', 'number'].includes(typeof options.level) ? options.level : defaultOptions.level; - opt.suffix = typeof options.suffix === 'string' ? options.suffix : defaultOptions.suffix; - opt.source = options.source || defaultOptions.source; - opt.sourceColor = options.sourceColor || defaultOptions.sourceColor; - try {if (client.config.logLevel < opt.level) {return;}} - catch { - client.config.logLevel = getLevel(client.config.logLevel); - if (client.config.logLevel < opt.level) {return;} - } - console.log(`${spacer ? '\n' : ''}${(opt.sourceColor.startsWith('#') ? chalk.hex(opt.sourceColor) : chalk[opt.sourceColor])(`[${opt.source.toUpperCase()}]`)}${opt.suffix}${options.nc || options.noColor ? message : (opt.color.startsWith('#') ? chalk.hex(opt.color) : chalk[opt.color])(message)}${newLine ? '\n' : ''}`); + return { + log: tlog(client), + ...types(tlog(client)) }; }; \ No newline at end of file diff --git a/src/util/log/types.js b/src/util/log/types.js index e69de29..ddb7525 100644 --- a/src/util/log/types.js +++ b/src/util/log/types.js @@ -0,0 +1,20 @@ +module.exports = (log) => { return { + error: (message, options, newLine, spacer) => log(message, { + color: 'redBright', + source: 'err!', + sourceColor: 'red', + level: 0 + }, newLine, spacer), + warn: (message, options, newLine, spacer) => log(message, { + color: 'yellowBright', + source: 'warn', + sourceColor: 'yellow', + level: 1 + }, newLine, spacer), + success: (message, options, newLine, spacer) => log(message, { + color: 'greenBright', + source: 'proc' || options && options.source, + sourceColor: 'green', + level: options && typeof options.level !== 'undefined' ? options.level : 0 + }, newLine, spacer) +}}; \ No newline at end of file