-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.js
101 lines (76 loc) · 1.96 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
var fs = require('fs');
function Logger(config) {
"use strict";
this._level_error = 0;
this._level_warning = 1;
this._level_notice = 2;
this._level_debug = 3;
this._file = null;
this._level = config.level;
this._debugKeys = config.debugKeys;
this._console = config.console;
if (null !== config.file) {
this._file = fs.createWriteStream(config.file, {
flags: 'a+'
});
this._file.on('error', function (err) {
console.log('Error on open log file: ', err.message);
});
}
}
Logger.prototype.destroy = function () {
"use strict";
if (null !== this._file) {
this._file.end();
}
};
Logger.prototype.error = function (msg) {
"use strict";
if (this._level < this._level_error) {
return;
}
this._logConsole('ERROR: ' + msg);
this._logFile('ERROR: ' + msg);
};
Logger.prototype.warning = function (msg) {
"use strict";
if (this._level < this._level_warning) {
return;
}
this._logConsole('WARNING: ' + msg);
this._logFile('WARNING: ' + msg);
};
Logger.prototype.notice = function (msg) {
"use strict";
if (this._level < this._level_notice) {
return;
}
this._logConsole('NOTICE: ' + msg);
this._logFile('NOTICE: ' + msg);
};
Logger.prototype.debug = function (key, msg) {
"use strict";
if (this._level < this._level_debug) {
return;
}
if (0 !== this._debugKeys.length
&& -1 === this._debugKeys.indexOf(key)
) {
return;
}
this._logConsole('DEBUG (' + key + '): ' + msg);
this._logFile('DEBUG (' + key + '): ' + msg);
};
Logger.prototype._logConsole = function (msg) {
"use strict";
if (true === this._console) {
console.log(msg);
}
};
Logger.prototype._logFile = function (msg) {
"use strict";
if (null !== this._file) {
this._file.write(msg + "\n", 'utf-8');
}
};
module.exports = Logger;