DiscordTickets/handlers/logger.js

89 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-05-05 18:34:39 +03:00
const fs = require('fs');
const leeks = require('leeks.js'); // ultra light weight alternative to chalk
2019-05-05 16:31:21 +03:00
const now = new Date();
2019-05-05 18:34:39 +03:00
let date = now.getDate();
2019-05-05 16:31:21 +03:00
const h = now.getHours();
const m = now.getMinutes();
const s = now.getSeconds();
let timestamp = `${h}:${('0' + m).slice(-2)}:${('0' + s).slice(-2)}`;
2019-05-05 18:34:39 +03:00
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));
}
};
2019-05-05 16:31:21 +03:00
2019-05-05 16:46:49 +03:00
exports.init = x => {
2019-05-05 18:34:39 +03:00
init(x)
2019-05-05 16:46:49 +03:00
};
2019-05-05 16:31:21 +03:00
exports.basic = (m) => {
2019-05-05 18:34:39 +03:00
console.log(`[${timestamp}] ${m}`)
fs.writeFileSync(path, `[${timestamp}] ${m}\n`, function (error) {
if (error) throw error;
});
2019-05-05 16:31:21 +03:00
};
exports.console = (m) => {
2019-05-05 16:46:49 +03:00
console.log(`[INFO | ${timestamp}] ${m}`);
2019-05-05 18:34:39 +03:00
let data = fs.readFileSync(path);
fs.writeFileSync(path, data + `[INFO | ${timestamp}] ${m}\n`, function (error) {
if (error) throw error;
});
2019-05-05 16:31:21 +03:00
};
exports.info = (m) => {
2019-05-05 16:46:49 +03:00
console.info(leeks.colours.cyan(`[INFO | ${timestamp}] ${m}`));
2019-05-05 18:34:39 +03:00
let data = fs.readFileSync(path);
fs.writeFileSync(path, data + `[INFO | ${timestamp}] ${m}\n`, function (error) {
if (error) throw error;
});
2019-05-05 16:31:21 +03:00
};
exports.success = (m) => {
2019-05-05 16:46:49 +03:00
console.info(leeks.colours.green(`[INFO | ${timestamp}] ${m}`));
2019-05-05 18:34:39 +03:00
let data = fs.readFileSync(path);
fs.writeFileSync(path, data + `[INFO | ${timestamp}] ${m}\n`, function (error) {
if (error) throw error;
});
2019-05-05 16:31:21 +03:00
};
exports.debug = (m) => {
2019-05-05 16:46:49 +03:00
console.info(leeks.colours.blueBright(`[DEBUG | ${timestamp}] ${m}`));
2019-05-05 18:34:39 +03:00
let data = fs.readFileSync(path);
fs.writeFileSync(path, data + `[DEBUG | ${timestamp}] ${m}\n`, function (error) {
if (error) throw error;
});
2019-05-05 16:31:21 +03:00
};
exports.warn = (m) => {
2019-05-05 16:46:49 +03:00
console.warn(leeks.colours.yellowBright(`[WARN | ${timestamp}] ${m}`));
2019-05-05 18:34:39 +03:00
let data = fs.readFileSync(path);
fs.writeFileSync(path, data + `[WARN | ${timestamp}] ${m}\n`, function (error) {
if (error) throw error;
});
2019-05-05 16:31:21 +03:00
};
exports.error = (m) => {
2019-05-05 16:46:49 +03:00
console.error(leeks.colours.red(`[ERROR | ${timestamp}] ${m}`));
2019-05-05 18:34:39 +03:00
let data = fs.readFileSync(path);
fs.writeFileSync(path, data + `[ERROR | ${timestamp}] ${m}\n`, function (error) {
if (error) throw error;
});
2019-05-05 16:31:21 +03:00
};