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 20:42:56 +03:00
|
|
|
|
|
|
|
function time() {
|
|
|
|
const n = new Date();
|
|
|
|
const h = n.getHours();
|
|
|
|
const m = n.getMinutes();
|
|
|
|
const s = n.getSeconds();
|
|
|
|
return `${h}:${('0' + m).slice(-2)}:${('0' + s).slice(-2)}`;
|
|
|
|
}
|
2019-05-05 18:34:39 +03:00
|
|
|
let d = ('0' + date).slice(-2);
|
2019-05-06 17:29:37 +03:00
|
|
|
if (!fs.existsSync('./logs')) {
|
|
|
|
fs.mkdirSync(`./logs`);
|
|
|
|
console.log(`[INFO | ${time()}] No logs directory found, creating one for you.`);
|
|
|
|
}
|
2019-05-05 18:34:39 +03:00
|
|
|
const files = fs.readdirSync('./logs/').filter(file => file.endsWith('.log'));
|
2019-05-06 17:29:37 +03:00
|
|
|
// let path = `./logs/${d}-${time().replace(':', '-').replace(':', '-')}.log`;
|
|
|
|
let path = `./logs/${d}-${('0' + now.getMonth()).slice(-2)}-${now.getFullYear()}.${time().replace(':', '-').replace(':', '-')}.log`;
|
2019-05-05 18:34:39 +03:00
|
|
|
// const log = require(path);
|
|
|
|
|
2019-05-05 20:42:56 +03:00
|
|
|
|
2019-05-05 18:34:39 +03:00
|
|
|
function init(x) {
|
2019-05-06 17:29:37 +03:00
|
|
|
// console.log(leeks.colours.cyan(`[INFO | ${time()}] Setting up...`));
|
2019-05-05 20:42:56 +03:00
|
|
|
console.log(`[INFO | ${time()}] Initialising logger`);
|
2019-05-06 17:29:37 +03:00
|
|
|
|
2019-05-05 20:42:56 +03:00
|
|
|
console.log(`[INFO | ${time()}] Cleaning up...`);
|
2019-05-05 18:34:39 +03:00
|
|
|
for (const file of files) {
|
2019-05-06 17:29:37 +03:00
|
|
|
// if(!file.startsWith(`${d}-`)) {
|
|
|
|
if(parseInt(file.substr(0,2)) < parseInt(d-7) || parseInt(file.substr(0,2)) > parseInt(d)) {
|
2019-05-05 18:34:39 +03:00
|
|
|
fs.unlinkSync(`./logs/${file}`)
|
2019-05-06 17:29:37 +03:00
|
|
|
console.log(`[INFO | ${time()}] Deleting './logs/${file}'`);
|
2019-05-05 18:34:39 +03:00
|
|
|
};
|
|
|
|
};
|
|
|
|
try {
|
2019-05-06 00:08:30 +03:00
|
|
|
if(x){
|
|
|
|
fs.appendFileSync(path, `${x} | Log File (${d}/${('0' + now.getMonth()).slice(-2)}/${now.getFullYear()}) -->\n`, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fs.appendFileSync(path, `Log File (${d}/${('0' + now.getMonth()).slice(-2)}/${now.getFullYear()}) -->\n`, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-05 20:42:56 +03:00
|
|
|
console.log(`[INFO | ${time()}] Creating new log file (${path})`);
|
2019-05-05 18:34:39 +03:00
|
|
|
} 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-06 17:29:37 +03:00
|
|
|
exports.basic = (m, c) => {
|
|
|
|
if(c){console.log(leeks.colours[c](`[${time()}] ${m}`))} else {console.log(`[${time()}] ${m}`)};
|
|
|
|
|
2019-05-05 20:42:56 +03:00
|
|
|
fs.writeFileSync(path, `[${time()}] ${m}\n`, function (error) {
|
2019-05-05 18:34:39 +03:00
|
|
|
if (error) throw error;
|
|
|
|
});
|
2019-05-05 16:31:21 +03:00
|
|
|
};
|
|
|
|
|
2019-05-06 17:29:37 +03:00
|
|
|
exports.console = (m, c) => {
|
|
|
|
if(c){console.log(leeks.colours[c](`[${time()}] ${m}`))} else {console.log(`[INFO | ${time()}] ${m}`)};
|
2019-05-05 18:34:39 +03:00
|
|
|
let data = fs.readFileSync(path);
|
2019-05-05 20:42:56 +03:00
|
|
|
fs.writeFileSync(path, data + `[INFO | ${time()}] ${m}\n`, function (error) {
|
2019-05-05 18:34:39 +03:00
|
|
|
if (error) throw error;
|
|
|
|
});
|
2019-05-05 16:31:21 +03:00
|
|
|
};
|
|
|
|
|
2019-05-06 17:29:37 +03:00
|
|
|
exports.info = (m, c) => {
|
|
|
|
if(c){console.info(leeks.colours[c](`[INFO | ${time()}] ${m}`))} else {console.info(leeks.colours.cyan(`[INFO | ${time()}] ${m}`))};
|
2019-05-05 18:34:39 +03:00
|
|
|
let data = fs.readFileSync(path);
|
2019-05-05 20:42:56 +03:00
|
|
|
fs.writeFileSync(path, data + `[INFO | ${time()}] ${m}\n`, function (error) {
|
2019-05-05 18:34:39 +03:00
|
|
|
if (error) throw error;
|
|
|
|
});
|
2019-05-05 16:31:21 +03:00
|
|
|
};
|
2019-05-06 17:29:37 +03:00
|
|
|
exports.success = (m, c) => {
|
|
|
|
if(c){console.info(leeks.colours[c](`[INFO | ${time()}] ${m}`))} else {console.info(leeks.colours.green(`[INFO | ${time()}] ${m}`))};
|
2019-05-05 18:34:39 +03:00
|
|
|
let data = fs.readFileSync(path);
|
2019-05-05 20:42:56 +03:00
|
|
|
fs.writeFileSync(path, data + `[INFO | ${time()}] ${m}\n`, function (error) {
|
2019-05-05 18:34:39 +03:00
|
|
|
if (error) throw error;
|
|
|
|
});
|
2019-05-05 16:31:21 +03:00
|
|
|
};
|
2019-05-06 17:29:37 +03:00
|
|
|
exports.debug = (m, c) => {
|
|
|
|
if(c){console.info(leeks.colours[c](`[DEBUG | ${time()}] ${m}`))} else {console.info(leeks.colours.blueBright(`[DEBUG | ${time()}] ${m}`))};
|
2019-05-05 18:34:39 +03:00
|
|
|
let data = fs.readFileSync(path);
|
2019-05-05 20:42:56 +03:00
|
|
|
fs.writeFileSync(path, data + `[DEBUG | ${time()}] ${m}\n`, function (error) {
|
2019-05-05 18:34:39 +03:00
|
|
|
if (error) throw error;
|
|
|
|
});
|
2019-05-05 16:31:21 +03:00
|
|
|
};
|
2019-05-06 17:29:37 +03:00
|
|
|
exports.warn = (m, c) => {
|
|
|
|
if(c){console.warn(leeks.colours[c](`[WARN | ${time()}] ${m}`))} else {console.warn(leeks.colours.yellowBright(`[WARN | ${time()}] ${m}`))};
|
2019-05-05 18:34:39 +03:00
|
|
|
let data = fs.readFileSync(path);
|
2019-05-05 20:42:56 +03:00
|
|
|
fs.writeFileSync(path, data + `[WARN | ${time()}] ${m}\n`, function (error) {
|
2019-05-05 18:34:39 +03:00
|
|
|
if (error) throw error;
|
|
|
|
});
|
2019-05-05 16:31:21 +03:00
|
|
|
};
|
2019-05-06 17:29:37 +03:00
|
|
|
exports.error = (m, c) => {
|
|
|
|
if(c){console.error(leeks.colours[c](`[ERROR | ${time()}] ${m}`))} else {console.error(leeks.colours.red(`[ERROR | ${time()}] ${m}`))};
|
2019-05-05 18:34:39 +03:00
|
|
|
let data = fs.readFileSync(path);
|
2019-05-05 20:42:56 +03:00
|
|
|
fs.writeFileSync(path, data + `[ERROR | ${time()}] ${m}\n`, function (error) {
|
2019-05-05 18:34:39 +03:00
|
|
|
if (error) throw error;
|
|
|
|
});
|
2019-05-05 16:31:21 +03:00
|
|
|
};
|
2019-05-06 17:29:37 +03:00
|
|
|
exports.custom = (t, m, c) => {
|
|
|
|
if(c){console.log(leeks.colours[c](`[${t} | ${time()}] ${m}`))} else {console.log(`[${t} | ${time()}] ${m}`)};
|
|
|
|
let data = fs.readFileSync(path);
|
|
|
|
fs.writeFileSync(path, data + `[${t} | ${time()}] ${m}\n`, function (error) {
|
|
|
|
if (error) throw error;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
module.exports.colors = leeks.colors;
|
|
|
|
module.exports.colours = leeks.colours;
|
|
|
|
module.exports.color = leeks.colors;
|
|
|
|
module.exports.colour = leeks.colours;
|