-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.js
56 lines (49 loc) · 1.63 KB
/
log.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
48
49
50
51
52
53
54
55
56
const fs = require("fs");
function timestamp(date = new Date()) {
const year = String(date.getFullYear()).padStart(4, "0");
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hour = String(date.getHours()).padStart(2, "0");
const minute = String(date.getMinutes()).padStart(2, "0");
const second = String(date.getSeconds()).padStart(2, "0");
return `[${year}-${month}-${day} ${hour}:${minute}:${second}]`;
}
function log(color, level, message) {
var output = `${color}${timestamp()} ${level}: ${message}`;
console.log(output);
writeToFile(output.replace(/\x1b\[.*?m/g, "") + "\n");
}
function writeToFile(text) {
fs.writeFile("./log.txt", text, { flag: "a+" }, (error) => {
if (error) console.log(error);
});
}
writeToFile(`-------- Start on ${timestamp().slice(1, -1)} --------\n`);
/*
* TRACE - extremely fine logging
* DEBUG - for debugging purposes
* INFO - for general information
* WARN - when something unexpected happens
* ERROR - when something goes wrong
* FATAL - when something really bad happens
*/
module.exports = {
trace: function (message) {
log("\x1b[2m", "TRACE\x1b[0m", message);
},
debug: function (message) {
log("\x1b[2m", "\x1b[0;32mDEBUG\x1b[0m", message);
},
info: function (message) {
log("\x1b[2m", "\x1b[0;36mINFO\x1b[0m", message);
},
warn: function (message) {
log("\x1b[0m", "\x1b[33mWARN\x1b[0m", message);
},
error: function (message) {
log("\x1b[1m", "\x1b[31mERROR\x1b[0m", message);
},
fatal: function (message) {
log("\x1b[1;37;41m", "FATAL", `${message}\x1b[0m`);
},
};