-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (133 loc) · 4.45 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Author: Conni!~#0920 (conni@spookiebois.club)
Github: https://github.com/ConniBug/JS-Logging
*/
const mailLib = require("./mailer");
const { getDateTime } = require("./Utils");
var callerId = require('caller-id');
var strip = require('strip-color');
const colors = require('colors');
var fileLogger = require("./fileLogger");
var sendMail = require("./mailer").sendMail;
var shouldMail = false;
var isMailSetup = false;
var isFileSetup = false;
function setupFileLogging(root) {
isFileSetup = true;
fileLogger.setupFileLogger(root);
}
function setupMail(host, port, email, email_pass, send_to) {
if(isMailSetup) log("Already setup email server.", "ERROR");
isMailSetup = true;
mailLib.setupMail(host, port, email, email_pass, send_to);
}
logLevel = "LEGITALL";
function getLogLevelNum(level) {
if (level == "TESTING") return 0;
if (level == "GENERIC") return 2;
if (level == "DEBUG") return 3;
if (level == "WARNING") return 5;
if (level == "ERROR") return 7;
if (level == "VERBOSE") return 9;
if (level == "CRITICAL") return 11;
if (level == "ALL") return 15;
// Debugging stuff.
if (level == "TIMINGS") return 20;
if (level == "LEGITALL") return 100;
log("Unsure what log level " + level.red + " belongs to.", "GENERIC");
return 4;
}
async function log(message, caller, type = "DEBUG", callingFunction = "N/A") {
if (getLogLevelNum(type) > getLogLevelNum(logLevel)) {
return;
}
maxSize = 54
time = getDateTime().yellow;
if (callingFunction == "N/A") {
StartMessage = `[${time}] - [`;
} else {
StartMessage = `[${time}] - [${callingFunction.blue}] - [`;
}
if (type == "ERROR") {
console.log(caller);
StartMessage += type.red;
if(isMailSetup) {
sendMail(message, "Server Error");
}
}
else if (type == "WARNING") StartMessage += type.blue;
else if (type == "GENERIC") StartMessage += type.green;
else if (type == "DEBUG") StartMessage += type.gray;
else if (type == "VERBOSE") StartMessage += type.rainbow;
else if (type == "TESTING") StartMessage += type.magenta;
else if (type == "CRITICAL") StartMessage += type.bgRed
else StartMessage += type.blue;
left = maxSize - StartMessage.length;
function balence() {
tmp = "";
space = " ";
while(left >= 0) {
left = left-1;
tmp = tmp + space;
}
return tmp;
}
let toLog = StartMessage + "] " + balence(StartMessage) + "-> " + message;
if(isFileSetup) {
fileLogger.logToFile(strip(toLog));
}
console.log(toLog);
}
function setupWebPanel() {
const express = require("express")
const app = express()
let PORT = 1321;
app.get("/latest", (req, res) => {
const fs = require("fs");
let data = fs.readFileSync(fileLogger.logFilePath, "utf8").toString().replace(/\n/g, "<br>");
res.send(data);
res.status(200);
});
app.listen(PORT, () => console.log(`Started listener on port ${PORT}`));
}
module.exports = {
setupWebPanel: () => {
return setupWebPanel();
},
setupFileLogging: (root) => {
return setupFileLogging(root);
},
setupMail: (host, port, email, email_pass, send_to) => {
return setupMail(host, port, email, email_pass, send_to);
},
setLogLevel: (Level) => {
logLevel = Level
},
getLogLevelNum: (level) => {
return getLogLevelNum(level);
},
log: async (message, type = "GENERIC", callingFunction = "N/A") => {
log(message, callerId.getData(), type, callingFunction);
},
verbose: async (message, callingFunction = "N/A") => {
log(message, callerId.getData(), "VERBOSE", callingFunction);
},
error: async (message, callingFunction = "N/A") => {
log(message, callerId.getData(), "ERROR", callingFunction);
},
warning: async (message, callingFunction = "N/A") => {
log(message, callerId.getData(), "WARNING", callingFunction);
},
debug: async (message, callingFunction = "N/A") => {
log(message, callerId.getData(), "DEBUG", callingFunction);
},
critical: async (message, callingFunction = "N/A") => {
log(message, callerId.getData(), "CRITICAL", callingFunction);
},
char_count: (str, letter) => {
return char_count(str, letter);
},
getDateTime: () => {
return getDateTime();
}
};