-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
82 lines (73 loc) · 1.94 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
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
const fs = require("fs");
// require("dotenv").config();
const tempLogFilePath = "./public/temp.txt";
const errorFilePath = "./public/errors.log";
const fullLogFilePath = "./public/logs.log";
const logStream = fs.createWriteStream(tempLogFilePath, { flags: "a" });
const logMsg = (msg) => {
console.log(msg);
logStream.write(msg + "\n");
};
// only fired when requested from telegram bot
const sendFullLogs = new Promise((resolve, reject) => {
resolve("to be fixed");
// fs.readFile(fullLogFilePath, (err, data) => {
// if (err) reject(err.toString());
// resolve(data.toString());
// });
});
const sendErrorLog = new Promise((resolve, reject) => {
resolve("to be fixed");
// fs.readFile(errorFilePath, (err, data) => {
// if (err) reject(err.toString());
// resolve(data.toString());
// });
});
const clearFullLogs = new Promise((resolve, reject) => {
fs.writeFile(fullLogFilePath, "", (err) => {
if (err) reject(err);
resolve("error log file cleared");
});
});
/////////////////////
const send = (bot) => {
return new Promise((resolve, reject) => {
try {
fs.readFile(tempLogFilePath, (err, data) => {
if (data.length > 1) {
bot.sendMessage(process.env.CHAT_ID, data.toString());
resolve();
}
});
} catch (error) {
reject(err);
}
});
};
const sendErrors = (bot) => {
sendErrorLog
.then((msg) => {
if (msg.length > 1) bot.sendMessage(process.env.CHAT_ID, msg);
})
.catch((err) => bot.sendMessage(process.env.CHAT_ID, err));
};
const sendLogs = async (bot) => {
send(bot)
.then(() => {
fs.writeFile(tempLogFilePath, "", () =>
console.log("temp log File cleared")
);
})
.then(() => sendErrors(bot))
.catch((err) => {
console.error(err);
bot.sendMessage(process.env.CHAT_ID, err.toString());
});
};
module.exports = {
logMsg,
sendLogs,
sendErrorLog,
sendFullLogs,
clearFullLogs,
};