Archived
0
0
Fork 0

feat(logging): added logging scopes

This commit is contained in:
Daryl Ronningen 2021-06-17 02:12:06 -07:00
parent c028cdd38a
commit 2534e50ef9
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
3 changed files with 63 additions and 21 deletions

View file

@ -14,15 +14,14 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>. * along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>.
*/ */
import { Client } from 'discord.js'; import { Client } from 'discord.js';
import figlet from 'figlet'; import figlet from 'figlet';
import gradient from 'gradient-string'; import gradient from 'gradient-string';
import { token } from '../config.json';
import { debug, error, info } from './utils/logger'; 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) => { figlet('Argon Bot', (err, data) => {
if (err) error(`Figlet encountered an error!\n${err.message}`); if (err) error(`Figlet encountered an error!\n${err.message}`);
@ -35,7 +34,7 @@ const client = new Client({
}); });
client.on('ready', () => { client.on('ready', () => {
info('bot is ready!'); info('Bot is ready!', ELoggingScope.Startup);
}); });
client.login(token); //client.login(token);

View file

@ -14,12 +14,12 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>. * along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>.
*/ */
import chalk from 'chalk'; import chalk from 'chalk';
import { DateTime } from 'luxon'; import { DateTime } from 'luxon';
import { loglevel } from '../../config.json'; import { loglevel } from '../../config.json';
import type { ELoggingScope } from './types';
const logger = new chalk.Instance({ level: 3 }); const logger = new chalk.Instance({ level: 3 });
const date = DateTime.now().toLocal().setLocale(Intl.DateTimeFormat().resolvedOptions().locale).toFormat('yyyy-LL-dd HH:mm:ss'); 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; break;
} }
export function verbose(message: string): void { export function verbose(message: string, scope?: ELoggingScope): void {
if(verboseLevel) 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) 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 { export function info(message: string, scope?: ELoggingScope): void {
if(infoLevel) if (infoLevel)
console.log(logger`{grey (${date})} {green.bold [INFO]}: ${message}`); 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) 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) 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) 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}}`);
} }

25
src/utils/types.ts Normal file
View file

@ -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 <https: //www.gnu.org/licenses/>.
*/
//Enums
export enum ELoggingScope {
Startup = 'STARTUP',
Command = 'COMMAND',
}
// Interfaces
// Type Aliases