-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
124 lines (117 loc) · 3.35 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// we support standard npm logging, i.e.:
// { error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }
var winston = require('winston');
var fs = require('fs');
var winstonLog;
var pastLogs = [];
var serverLog = {
silly: function (message) {
winstonLog.silly(message);
},
debug: function (message) {
winstonLog.debug(message);
},
info: function (message) {
winstonLog.info(message);
},
verbose: function (message) {
winstonLog.verbose(message);
},
warn: function (message) {
winstonLog.warn(message);
},
error: function (message) {
winstonLog.error(message);
},
admin: function (message) {
winstonLog.info(message);
addToPastLogsAndSentToAdmins(message);
},
log: function (loglevel, message) {
switch (loglevel) {
case "silly":
serverLog.silly(message);
break;
case "debug":
serverLog.debug(message);
break;
case "info":
serverLog.info(message);
break;
case "verbose":
serverLog.verbose(message);
break;
case "warn":
serverLog.warn(message);
break;
case "error":
serverLog.error(message);
break;
case "admin":
serverLog.admin(message);
break;
default:
winstonLog.warn("Don\'t know loglevel " + loglevel);
}
},
newAdmin: function () {
return pastLogs;
}
};
/**
* Manages the messages in pastLogs and informs the admins about the new message
* @param information last sent message
*/
function addToPastLogsAndSentToAdmins(message) {
var information = {
message: message,
time: Date.now()
};
pastLogs.push(information);
if (pastLogs.length > 30) pastLogs.shift();
messageHandler.sendAdminsLogInformation(false, information);
}
/**
* Initialize server log with specified logLevel for console logs.
* @param logLevel
*/
exports.initServerLog = function initServerLog(logLevel, logDir) {
// create requested log file if necessary
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir);
}
winstonLog = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
name: 'console',
level: logLevel
}),
new (winston.transports.File)({
name: 'info-file',
filename: logDir + '/serverLog.log',
level: 'info'
}),
new (winston.transports.File)({
name: 'error-file',
filename: logDir + '/crash.log',
level: 'error',
handleExceptions: true,
humanReadableUnhandledException: true
})
]
});
};
/**
* Get central server log. If the log was not yet initialized, it will be created with default
* 'info' log level.
* @returns {*}
*/
exports.getServerLog = function () {
if (!serverLog) {
// init standard logger if not yet initialized
initServerLog('info');
}
return serverLog;
};
//message handler. needed to send log information to admins
var messageHandler = require('./messageHandler');