-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
46 lines (35 loc) · 1.04 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
(function() {
const fs = require('fs');
const schedule = require('node-schedule');
var filename = "logs/" + getFilename();
// flag a: append
// flag s: synchron
var stream = fs.createWriteStream(filename, {flags:'as'});
stream.write("\n" + new Date().toISOString() + ":\tStarting Frontend...\n");
stream.on('error', function (err) {
console.log("LOGGER ERROR!");
console.log(err);
});
schedule.scheduleJob('0 0 0 * * *', function(){
filename = "logs/" + getFilename();
const newStream = fs.createWriteStream(filename, {flags:'as'});
stream.end();
stream = newStream;
});
exports.log = function(text) {
stream.write(new Date().toISOString() + ":\t" + text + "\n");
console.log(text);
};
exports.close = function() {
stream.end();
};
function getFilename() {
const date = new Date();
const year = date.getFullYear();
var month = date.getMonth()+1;
if (month < 10) month = "0"+ month;
var day = date.getDate();
if (day < 10) day = "0"+ day;
return day + "_" + month + "_" + year + ".txt";
}
}());