-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogging.js
45 lines (40 loc) · 1.4 KB
/
logging.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
const fs = require("fs/promises");
const path = require("path");
class botLog {
constructor(name, toFile, filepath){
this.name = name;
this.toFile = toFile;
if (toFile){
const absolutePath = path.resolve(__dirname, filepath);
fs.access(absolutePath).then(() =>{
fs.stat(absolutePath).then((pathStats) => {
if (pathStats.isDirectory()){
this.filepath = filepath + `/${name} - ${SafeWindowsFilename()}.log`;
}
else {
this.filepath = filepath;
}
});
})
.catch((err) => {
throw new Error("Logging file/directory doesn't exist!");
});
}
}
async log(type, message){
let time = new Date(Date.now()).toISOString();
if (type !== "ERROR"){
console.log(`[${time} | ${type}] ${this.name}: ${message}`);
}
else {
console.error(`[${time} | ${type}] ${this.name}: ${message}`);
}
if (this.toFile){
await fs.writeFile(this.filepath, `[${time} | ${type}] ${this.name}: ${message}\n`, {flag: "a"});
}
}
}
function SafeWindowsFilename(){
return new Date(Date.now()).toISOString().replace(/:/g, "-").replace("T", "_").slice(0,19);
}
module.exports = {botLog};