Improve logging

This commit is contained in:
Isaac
2021-02-27 21:13:46 +00:00
parent ddba4e2482
commit e53427cb36
9 changed files with 261 additions and 53 deletions

View File

@@ -60,32 +60,7 @@ const config = require('../user/config');
require('./banner')();
const Logger = require('leekslazylogger-fastify');
const log = new Logger({
name: 'DiscordTickets by eartharoid',
debug: config.debug,
logToFile: config.logs.enabled,
directory: path('./logs/'),
keepFor: config.logs.keep_for,
custom: {
commands: {
title: 'info',
prefix: 'commands'
},
http: {
title: 'info',
prefix: 'http'
},
plugins: {
title: 'info',
prefix: 'plugins'
},
tickets: {
title: 'info',
prefix: 'tickets'
}
}
});
const log = require('./logger');
const { selectPresence } = require('./utils/discord');
const I18n = require('@eartharoid/i18n');

View File

@@ -1,7 +1,7 @@
module.exports = {
event: 'guildCreate',
execute: async (client, guild) => {
client.log.console(`Added to ${guild.name}`);
client.log.info(`Added to ${guild.name}`);
await guild.createSettings();
}
};

View File

@@ -1,7 +1,7 @@
module.exports = {
event: 'guildDelete',
execute: async (client, guild) => {
client.log.console(`Removed from ${guild.name}`);
client.log.info(`Removed from ${guild.name}`);
await guild.deleteSettings();
}
};

56
src/logger.js Normal file
View File

@@ -0,0 +1,56 @@
const { path } = require('./utils/fs');
const config = require('../user/config');
const Logger = require('leekslazylogger-fastify');
module.exports = new Logger({
name: 'DiscordTickets by eartharoid',
debug: config.debug,
logToFile: config.logs.enabled,
splitFile: true,
directory: path('./logs/'),
keepFor: config.logs.keep_for,
levels: {
_logger: {
format: '&7[{timestamp}]&r [LOGGER] {text}'
},
basic: {
format: '[{timestamp}] {text}'
},
console: {
format: '[{timestamp}] [INFO] {text}'
},
info: {
format: '&7[{timestamp}]&r &3[INFO] &b{text}'
},
success: {
format: '&7[{timestamp}]&r &2[SUCCESS] &a{text}'
},
debug: {
format: '&7[{timestamp}]&r &1[DEBUG] &9{text}'
},
notice: {
format: '&7[{timestamp}]&r &0&!6[NOTICE] {text}'
},
warn: {
format: '&7[{timestamp}]&r &6[WARN] &e{text}'
},
error: {
format: '&7[{timestamp}]&r &4[ERROR] &c{text}'
},
commands: {
type: 'info',
format: '&7[{timestamp}]&r &3[INFO] &d(COMMANDS)&r {text}'
},
http: {
type: 'info',
format: '&7[{timestamp}]&r &3[INFO] &d(HTTP)&r {text}'
},
plugins: {
type: 'info',
format: '&7[{timestamp}]&r &3[INFO] &d(PLUGINS)&r {text}'
},
tickets: {
type: 'info',
format: '&7[{timestamp}]&r &3[INFO] &d(TICKETS)&r {text}'
}
}
});

View File

@@ -1,4 +1,7 @@
const { Plugin } = require('../modules/plugins');
const fastify = require('fastify');
const { randomBytes } = require('crypto');
const { path } = require('../utils/fs');
module.exports = class SettingsServer extends Plugin {
constructor(client) {
@@ -9,7 +12,7 @@ module.exports = class SettingsServer extends Plugin {
commands: []
});
this.fastify = require('fastify')();
this.fastify = fastify();
this.client.plugins.plugins.set(this.id, this);
this.preload();
@@ -17,19 +20,38 @@ module.exports = class SettingsServer extends Plugin {
async preload() {
this.fastify.register(this.client.log.fastify, {
type: 'http'
level: 'http',
format: '{method} {status-colour}{status} &7{route} {time-colour}({time})'
});
this.fastify.register(require('fastify-secure-session'), {
key: randomBytes(48).toString('hex')
});
this.fastify.register(require('fastify-static'), {
root: path('./src/server/public'),
prefix: '/public/',
});
}
async load() {
this.fastify.listen(process.env.HTTP_PORT || 8080, (err, address) => {
if (err) throw err;
this.client.log.info(`Settings server listening at ${address}`);
});
let host = process.env.HTTP_HOST;
if (!host.endsWith('/')) host = host + '/';
let redirect_url = encodeURI(`${host}auth/callback`);
let oauth2_url = `https://discord.com/api/oauth2/authorize?client_id=${this.client.user.id}&redirect_uri=${redirect_url}&response_type=code&scope=identify%20guilds&state=apollo`;
this.fastify.get('/', async (req, res) => {
res.type('application/json').code(200);
return { hello: 'world' };
// res.type('application/json').code(200);
// return { hello: 'world' };
res.code(200);
return 'Hello!';
});
this.fastify.listen(process.env.HTTP_PORT || 8080, (err, host) => {
if (err) throw err;
this.client.log.info(`Settings server listening at ${host}`);
});
}
};

View File