From 2534e50ef9c74dbe1e183201ba9b7025ba6dbab5 Mon Sep 17 00:00:00 2001 From: Daryl Ronningen Date: Thu, 17 Jun 2021 02:12:06 -0700 Subject: [PATCH] feat(logging): added logging scopes --- src/index.ts | 11 +++++------ src/utils/logger.ts | 48 +++++++++++++++++++++++++++++++-------------- src/utils/types.ts | 25 +++++++++++++++++++++++ 3 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 src/utils/types.ts diff --git a/src/index.ts b/src/index.ts index ed5d416..17ce9b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,15 +14,14 @@ * You should have received a copy of the GNU General Public License * along with ArgonBot. If not, see . */ - import { Client } from 'discord.js'; import figlet from 'figlet'; import gradient from 'gradient-string'; - -import { token } from '../config.json'; import { debug, error, info } from './utils/logger'; +import { ELoggingScope } from './utils/types'; +//import { token } from '../config.json'; -debug('Starting bot... Please wait!'); +debug('Starting bot... Please wait!', ELoggingScope.Startup); figlet('Argon Bot', (err, data) => { if (err) error(`Figlet encountered an error!\n${err.message}`); @@ -35,7 +34,7 @@ const client = new Client({ }); client.on('ready', () => { - info('bot is ready!'); + info('Bot is ready!', ELoggingScope.Startup); }); -client.login(token); +//client.login(token); diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 673540b..04b2cfa 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -14,12 +14,12 @@ * You should have received a copy of the GNU General Public License * along with ArgonBot. If not, see . */ - import chalk from 'chalk'; import { DateTime } from 'luxon'; - import { loglevel } from '../../config.json'; +import type { ELoggingScope } from './types'; + const logger = new chalk.Instance({ level: 3 }); const date = DateTime.now().toLocal().setLocale(Intl.DateTimeFormat().resolvedOptions().locale).toFormat('yyyy-LL-dd HH:mm:ss'); @@ -68,32 +68,50 @@ switch (loglevel.toLowerCase()) { break; } -export function verbose(message: string): void { +export function verbose(message: string, scope?: ELoggingScope): void { if(verboseLevel) - console.log(logger`{grey (${date})} {white.bold [VERBOSE]}: ${message}`); + if(scope) + console.log(logger`{grey (${date})} {magenta.bold ${scope}} {white.bold [VERBOSE]}: {white ${message}}`); + else + console.log(logger`{grey (${date})} {white.bold [VERBOSE]}: {white ${message}}`); } -export function debug(message: string): void { +export function debug(message: string, scope?: ELoggingScope): void { if(debugLevel) - console.log(logger`{grey (${date})} {blue.bold [DEBUG]}: ${message}`); + if(scope) + console.log(logger`{grey (${date})} {magenta.bold ${scope}} {blue.bold [DEBUG]}: {blue ${message}}`); + else + console.log(logger`{grey (${date})} {blue.bold [DEBUG]}: {blue ${message}}`); } -export function info(message: string): void { - if(infoLevel) - console.log(logger`{grey (${date})} {green.bold [INFO]}: ${message}`); +export function info(message: string, scope?: ELoggingScope): void { + if (infoLevel) + if(scope) + console.log(logger`{grey (${date})} {magenta.bold ${scope}} {green.bold [INFO]}: {green ${message}}`); + else + console.log(logger`{grey (${date})} {green.bold [INFO]}: {green ${message}}`); } -export function warn(message: string): void { +export function warn(message: string, scope?: ELoggingScope): void { if(warnLevel) - console.log(logger`{grey (${date})} {yellow.bold [WARN]}: ${message}`); + if(scope) + console.log(logger`{grey (${date})} {magenta.bold ${scope}} {yellow.bold [WARN]}: {yellow ${message}}`); + else + console.log(logger`{grey (${date})} {yellow.bold [WARN]}: {yellow ${message}}`); } -export function error(message: string): void { +export function error(message: string, scope?: ELoggingScope): void { if(errorLevel) - console.log(logger`{grey (${date})} {orange.bold.underline [ERROR]}: ${message}`); + if(scope) + console.log(logger`{grey (${date})} {magenta.bold ${scope}} {bold.underline.rgb(255, 165, 0) [ERROR]}: {underline.rgb(255, 165, 0) ${message}}`); + else + console.log(logger`{grey (${date})} {bold.underline.rgb(255, 165, 0) [ERROR]}: {underline.rgb(255, 165, 0) ${message}}`); } -export function fatal(message: string): void { +export function fatal(message: string, scope?: ELoggingScope): void { if(fatalLevel) - console.log(logger`{grey (${date})} {red.bold.underline [FATAL]}: ${message}`); + if(scope) + console.log(logger`{grey (${date})} {magenta.bold ${scope}} {red.bold.underline [FATAL]}: {red.underline ${message}}`); + else + console.log(logger`{grey (${date})} {red.bold.underline [FATAL]}: {red.underline ${message}}`); } diff --git a/src/utils/types.ts b/src/utils/types.ts new file mode 100644 index 0000000..3ee5a99 --- /dev/null +++ b/src/utils/types.ts @@ -0,0 +1,25 @@ +/* + * This file is part of ArgonBot + * + * ArgonBot is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ArgonBot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ArgonBot. If not, see . + */ +//Enums +export enum ELoggingScope { + Startup = 'STARTUP', + Command = 'COMMAND', +} + +// Interfaces + +// Type Aliases