custom log types

v2
Kit Kasune 2 years ago
parent d469ed3969
commit 31d5741707
  1. 10
      bot.js
  2. 32
      src/util/log/log.js
  3. 20
      src/util/log/types.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'});
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!");

@ -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))
};
};

@ -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)
}};
Loading…
Cancel
Save