forked from SOELexicon/burst-pool
-
Notifications
You must be signed in to change notification settings - Fork 1
/
burst-pool-logger.js
85 lines (78 loc) · 2.34 KB
/
burst-pool-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
83
84
85
var fs = require('fs');
var enabled = false;
var toConsole = false;
var toFile = true;
var toGeneralFile = false;
var generalLogFilename = 'log_general.txt';
function TimeStamp() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + "-" + month + "-" + day + "_" + hour + "-" + min + "-" + sec;
}
function TimeStampFile() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return day+ "-" + month + "-" + year;
}
function write(name,text){
if (enabled){
if (toFile){
fs.appendFile('log_'+name+'.txt', TimeStamp()+' '+text+'\n', function (err) {
if (err) throw err;
});
}
if (toGeneralFile){
fs.appendFile(generalLogFilename, TimeStamp()+' ('+name+') '+text+'\n', function (err) {
if (err) throw err;
});
}
if (toConsole){
console.log('(logger.'+name+") "+text)
}
}
}
function writeDateLog(text){
if (enabled){
if (toFile){
fs.exists('./Logs/log_'+TimeStampFile()+'.txt', function(exists) {
if (exists) {
fs.appendFile('./Logs/log_'+TimeStampFile()+'.txt', TimeStamp()+' '+text+'\n', function (err) {
if (err) throw err;
});
} else {
fs.writeFile('./Logs/log_'+TimeStampFile()+'.txt', TimeStamp()+' '+text+'\n', function(err) {
if(err) {
return console.log(err);
}
// console.log("The file was saved!");
});
}
});
}
}
}
module.exports = {
write : write,
writeDateLog:writeDateLog,
TimeStamp : TimeStamp
};