-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.js
More file actions
37 lines (33 loc) · 1.01 KB
/
Logger.js
File metadata and controls
37 lines (33 loc) · 1.01 KB
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
let _logger = false;
class Logger {
constructor(config) {
this.lvl = config.log_lvl;
this.self_lvl = config.self_log_lvl;
this.pattern = config.pattern;
if(config.log_amqp){
this.channel = new (require('@voicenter-team/failover-amqp-pool'))(config.log_amqp);
this.channel.start();
}
}
log(lvl, message) {
let date = new Date().toISOString();
if (lvl <= this.self_lvl && typeof message === 'object') {
let _message = JSON.stringify(Object.assign({}, this.pattern, { DateTime: date }, message));
console.log(_message);
}
if (this.channel && lvl <= this.lvl && typeof message === 'object') {
let _message = JSON.stringify(Object.assign({}, this.pattern, { DateTime: date }, message));
this.channel.publish(_message);
}
}
}
module.exports = function (config) {
if (_logger instanceof Logger) {
return _logger;
}
if (config) {
_logger = new Logger(config);
return _logger;
}
throw new Error("No logger configuration provided");
};