forked from fancoder/cryptonote-universal-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.js
65 lines (49 loc) · 1.68 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
var fs = require('fs');
var util = require('util');
var dateFormat = require('dateformat');
var clc = require('cli-color');
var severityMap = {
'info': clc.blue,
'warn': clc.yellow,
'error': clc.red
};
var severityLevels = ['info', 'warn', 'error'];
var logDir = config.logging.files.directory;
if (!fs.existsSync(logDir)){
try {
fs.mkdirSync(logDir);
}
catch(e){
throw e;
}
}
var pendingWrites = {};
setInterval(function(){
for (var fileName in pendingWrites){
var data = pendingWrites[fileName];
fs.appendFile(fileName, data);
delete pendingWrites[fileName];
}
}, config.logging.files.flushInterval * 1000);
global.log = function(severity, system, text, data){
var logConsole = severityLevels.indexOf(severity) >= severityLevels.indexOf(config.logging.console.level);
var logFiles = severityLevels.indexOf(severity) >= severityLevels.indexOf(config.logging.files.level);
if (!logConsole && !logFiles) return;
var time = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss');
var formattedMessage = text;
if (data) {
data.unshift(text);
formattedMessage = util.format.apply(null, data);
}
if (logConsole){
if (config.logging.console.colors)
console.log(severityMap[severity](time) + clc.white.bold(' [' + system + '] ') + formattedMessage);
else
console.log(time + ' [' + system + '] ' + formattedMessage);
}
if (logFiles) {
var fileName = logDir + '/' + system + '_' + severity + '.log';
var fileLine = time + ' ' + formattedMessage + '\n';
pendingWrites[fileName] = (pendingWrites[fileName] || '') + fileLine;
}
};