-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileLogger.js
59 lines (48 loc) · 1.42 KB
/
fileLogger.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
const cron = require('node-cron');
const { getDateTime } = require("./Utils");
const fs = require('fs');
var logFilePath = "./";
function makeDirIfNotExist(path) {
if(!fs.existsSync(path)) {
fs.mkdirSync(path, { recursive: true })
}
}
function getNewLogFilePath(root, date = new Date()) {
let path = root + "./logs/" + date.getFullYear();
let month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
path += "/" + month;
let day = date.getDate();
day = (day < 10 ? "0" : "") + day;
path += "/" + day + "/";
makeDirIfNotExist(path);
return path;
}
function getNewLogFileName(date = new Date()) {
return "./" + date.getTime() + ".log";
}
module.exports.setupFileLogger = async (root) => {
if(!root) {
console.error("[fileLogger.js] root dir not defined.")
process.exit(1)
}
let curDate = new Date()
logPath = getNewLogFilePath(root, curDate);
logFileName = getNewLogFileName(curDate);
logFilePath = logPath + logFileName;
return true;
}
module.exports.logFilePath = "sss";
module.exports.logToFile = async (content) => {
try {
this.logFilePath = logFilePath;
fs.appendFileSync(logFilePath, content + "\n")
} catch (err) {
console.error(err)
}
}
// Setup a new file to log to every X
var task = cron.schedule('0 0 */1 * * *', () => {
this.setupFileLogger("./");
});
task.start();