-
Notifications
You must be signed in to change notification settings - Fork 45
/
Logger.js
47 lines (40 loc) · 1.78 KB
/
Logger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const chalk = require('chalk');
const fs = require('fs');
const path = require("path");
Date.prototype.format = function(fmt) {
let o = {
"M+" : this.getMonth()+1,
"d+" : this.getDate(),
"h+" : this.getHours(),
"m+" : this.getMinutes(),
"s+" : this.getSeconds(),
"q+" : Math.floor((this.getMonth()+3)/3),
"S" : this.getMilliseconds()
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(let k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}
const LogFileName = new Date().format("yyyy-MM-dd") + ".txt";
if(!fs.existsSync("Logs")){
fs.mkdirSync("Logs");
}
function LogError(str){
console.log(chalk.redBright("[" + new Date().format("yyyy-MM-dd hh:mm:ss") + "][Error] ") + str);
fs.appendFileSync(path.join(__dirname,"./Logs/" + LogFileName), "[" + new Date().format("yyyy-MM-dd hh:mm:ss") + "][Error] " + str + "\n");
}
function LogInfo(str){
console.log(chalk.green("[" + new Date().format("yyyy-MM-dd hh:mm:ss") + "][Info] ") + str);
fs.appendFileSync(path.join(__dirname,"./Logs/" + LogFileName), "[" + new Date().format("yyyy-MM-dd hh:mm:ss") + "][Info] " + str + "\n");
}
function LogWarn(str){
console.log(chalk.yellow("[" + new Date().format("yyyy-MM-dd hh:mm:ss") + "][Warn] ") + str);
fs.appendFileSync(path.join(__dirname,"./Logs/" + LogFileName), "[" + new Date().format("yyyy-MM-dd hh:mm:ss") + "][Warn] " + str + "\n");
}
module.exports = { LogInfo,LogWarn,LogError };