From 64eeb50b0544887c44dd270a444b7967a87d9f13 Mon Sep 17 00:00:00 2001 From: Eartharoid Date: Sun, 5 May 2019 16:34:39 +0100 Subject: [PATCH] update logger :tm: --- example-config.json | 2 +- handlers/logger.js | 61 ++++++++++++++++++++++++++++++++++++++++++--- index.js | 14 +++++------ 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/example-config.json b/example-config.json index fa2406e..83a2d84 100644 --- a/example-config.json +++ b/example-config.json @@ -10,7 +10,7 @@ "playing": "with tickets (!help)", "useEmbeds": true, "logDMs": true, - "cooldown": 5, + "cooldown": 3, "ticketImage": true, "tagHereOnly": false, "ticketText": "Thanks you for reaching out to our support team.\nA member of staff will come to assist you shortly. Please describe the issue in detail and explain what you have done to resolve the issue so far.\n\n*If you feel that the support you recieve is inadequate, please feel free to submit a formal complaint to a senior member of staff.*", diff --git a/handlers/logger.js b/handlers/logger.js index 2820883..b1e168e 100644 --- a/handlers/logger.js +++ b/handlers/logger.js @@ -1,35 +1,88 @@ -const leeks = require('leeks.js'); +const fs = require('fs'); +const leeks = require('leeks.js'); // ultra light weight alternative to chalk const now = new Date(); -const d = now.getDate(); +let date = now.getDate(); const h = now.getHours(); const m = now.getMinutes(); const s = now.getSeconds(); let timestamp = `${h}:${('0' + m).slice(-2)}:${('0' + s).slice(-2)}`; +let d = ('0' + date).slice(-2); + +const files = fs.readdirSync('./logs/').filter(file => file.endsWith('.log')); +let path = `./logs/${d}-${timestamp.replace(':', '-').replace(':', '-')}.log`; +// const log = require(path); + +function init(x) { + console.log(leeks.colours.cyan(`[INFO | ${timestamp}] Setting up...`)); + console.log(`[INFO | ${timestamp}] Initialising logger`); + console.log(`[INFO | ${timestamp}] Cleaning up...`); + for (const file of files) { + if(!file.startsWith(`${d}-`) && !file.startsWith(`${d-1}-`)) { + fs.unlinkSync(`./logs/${file}`) + console.log(`[INFO | ${timestamp}] Deleting ./logs/${file}`); + }; + }; + try { + fs.appendFileSync(path, `Discord Tickets | Log File (${timestamp})\n`, function (error) { + if (error) throw error; + }); + console.log(`[INFO | ${timestamp}] Creating new log file (${path})`); + } catch(error) { + console.error(leeks.colours.red(error)); + } +}; exports.init = x => { - console.log(`INIT FUNCTION TRIGGERED`) + init(x) }; exports.basic = (m) => { - console.log(m) + console.log(`[${timestamp}] ${m}`) + fs.writeFileSync(path, `[${timestamp}] ${m}\n`, function (error) { + if (error) throw error; + }); }; exports.console = (m) => { console.log(`[INFO | ${timestamp}] ${m}`); + let data = fs.readFileSync(path); + fs.writeFileSync(path, data + `[INFO | ${timestamp}] ${m}\n`, function (error) { + if (error) throw error; + }); }; exports.info = (m) => { console.info(leeks.colours.cyan(`[INFO | ${timestamp}] ${m}`)); + let data = fs.readFileSync(path); + fs.writeFileSync(path, data + `[INFO | ${timestamp}] ${m}\n`, function (error) { + if (error) throw error; + }); }; exports.success = (m) => { console.info(leeks.colours.green(`[INFO | ${timestamp}] ${m}`)); + let data = fs.readFileSync(path); + fs.writeFileSync(path, data + `[INFO | ${timestamp}] ${m}\n`, function (error) { + if (error) throw error; + }); }; exports.debug = (m) => { console.info(leeks.colours.blueBright(`[DEBUG | ${timestamp}] ${m}`)); + let data = fs.readFileSync(path); + fs.writeFileSync(path, data + `[DEBUG | ${timestamp}] ${m}\n`, function (error) { + if (error) throw error; + }); }; exports.warn = (m) => { console.warn(leeks.colours.yellowBright(`[WARN | ${timestamp}] ${m}`)); + let data = fs.readFileSync(path); + fs.writeFileSync(path, data + `[WARN | ${timestamp}] ${m}\n`, function (error) { + if (error) throw error; + }); }; exports.error = (m) => { console.error(leeks.colours.red(`[ERROR | ${timestamp}] ${m}`)); + let data = fs.readFileSync(path); + fs.writeFileSync(path, data + `[ERROR | ${timestamp}] ${m}\n`, function (error) { + if (error) throw error; + }); }; diff --git a/index.js b/index.js index c4a505d..5a65718 100644 --- a/index.js +++ b/index.js @@ -50,8 +50,7 @@ const cooldowns = new Discord.Collection(); const now = Date.now(); const commands = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); -log.init() -log.basic(leeks.colours.magentaBright(` +console.log(leeks.colours.magentaBright(` ######## #### ###### ###### ####### ######## ######## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## @@ -69,11 +68,12 @@ log.basic(leeks.colours.magentaBright(` ## #### ###### ## ## ######## ## ###### `)); // banner appears in console -log.basic(leeks.colours.yellow(leeks.styles.bold(`DiscordTickets v${version} - Made By Eartharoid`))); -log.basic(leeks.colours.yellow(leeks.styles.bold(homepage))); -log.basic('\n\n'); -log.basic(leeks.colours.bgGrey(leeks.colours.grey(`\n\n==========================================================================\n\n`))) -log.basic('\n\n'); +console.log(leeks.colours.yellow(leeks.styles.bold(`DiscordTickets v${version} - Made By Eartharoid`))); +console.log(leeks.colours.yellow(leeks.styles.bold(homepage))); +console.log('\n\n'); +console.log(leeks.colours.bgGrey(leeks.colours.grey(`\n\n==========================================================================\n\n`))) +console.log('\n\n'); +log.init() log.info(`Starting up...`)