-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.js
74 lines (67 loc) · 2 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const fs = require('node:fs');
async function logPath(path, name) {
if (name == null) {
var date = new Date();
var name = date.getFullYear() + "-" + date.getMonth() + "-" + date.getDate() + ".log";
}
var file = path + name;
if (!fs.existsSync(path)){
fs.mkdirSync(path);
}
if (!fs.existsSync(file)){
fs.writeFile(file, '', function (err) {
if (err) throw err;
});
}
return file
}
async function writeLog(file, data) {
if (!fs.existsSync(file)){
await logPath(path, name);
}
fs.appendFile(file, data, function (err) {
if (err) throw err;
});
return true
}
/**
* The normal log function. For debug/status messages
* @param {string} message The message to be logged
* @param {string} func The name of the function that called this
* @param {boolean} toLogFile Weather to log it to a file or not
* @returns
*/
async function consoleMessage(message, func, toLogFile) {
let date = new Date();
let time = date.toTimeString().split(' ')[0];
if (func == null) {var func = "main"}
let messageOut = func + "@" + time + ": " + message
console.log(messageOut)
if (toLogFile) {
writeLog(messageOut, logPath())
}
return
}
/**
* The error function. For any type of error
* @param {string} message The message to be logged
* @param {string} func The name of the function that called this
* @param {boolean} toLogFile Weather to log it to a file or not
* @param {boolean} fatal Weather to exit the program or not
* @returns
*/
async function consoleError(message, func, toLogFile, fatal=false) {
let date = new Date();
let time = date.toTimeString().split(' ')[0];
if (func == null) {var func = "main"}
let messageOut = "ERROR from " + func + "@" + time + ": " + message
console.log(messageOut)
if (toLogFile) {
writeLog(messageOut, logPath())
}
return
}
module.exports = {
consoleError,
consoleMessage
}