feat(logging): added logging scopes
This commit is contained in:
parent
c028cdd38a
commit
2534e50ef9
3 changed files with 63 additions and 21 deletions
11
src/index.ts
11
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 <https: //www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
|
|
|
@ -14,12 +14,12 @@
|
|||
* You should have received a copy of the GNU General Public License
|
||||
* along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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}}`);
|
||||
}
|
||||
|
|
25
src/utils/types.ts
Normal file
25
src/utils/types.ts
Normal 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
|
Reference in a new issue