-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogManager.js
87 lines (66 loc) · 2.4 KB
/
LogManager.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
/*
LogManager: Using Slack and logger to log errors.
Created By: Ben goldenberg ([email protected])
*/
var Slack = require('slack-node');
var LogManager = {};
LogManager.msgSlack = false;
LogManager.msgLogger = false;
LogManager.Method = 'slack';
LogManager.slackDevChannel = '#dev';
LogManager.slackProdChannel = '#production';
LogManager.webhookUri = "";
LogManager.slack = new Slack();
LogManager.slack.setWebhook(LogManager.webhookUri);
LogManager.env = 'development';
/*
Configuration method
*/
LogManager.config = function (options){
LogManager.env = process.env.NODE_ENV;
if (options.env != undefined) LogManager.env = options.env;
if (options.logger != undefined) LogManager.logger = options.logger;
if (options.method != undefined) LogManager.Method = options.method;
if (options.msgSlack != undefined) LogManager.msgSlack = options.msgSlack;
if (options.msgLogger != undefined) LogManager.msgLogger = options.msgLogger;
if (options.devChannel != undefined) LogManager.slackDevChannel = options.devChannel;
if (options.prodChannel != undefined) LogManager.slackProdChannel = options.prodChannel;
if (options.webhookUri != undefined) LogManager.webhookUri = options.webhookUri;
//if (LogManager.env != 'production') LogManager.env = 'development';
};
LogManager.restart = function (){
if (LogManager.env == "development") return;
var msg = 'The server on ' + LogManager.env + ' was restarted';
if (LogManager.msgLogger) this.logger.info(msg);
if (LogManager.GlobalMsg && LogManager.Method === 'slack'){
LogManager.msgSlack(msg);
}
};
LogManager.error = function(msg){
msg = JSON.stringify(msg);
console.log('ERROR: ' + msg);
if (LogManager.msgLogger) this.logger.error(msg);
if (LogManager.GlobalMsg && LogManager.Method === 'slack'){
LogManager.msgSlack('ERROR: ' + msg);
}
};
LogManager.info = function(msg){
console.log('INFO: ' + msg);
if (LogManager.msgLogger) this.logger.info(msg);
};
LogManager.msgSlack = function(text){
var channel;
if (LogManager.env === 'production'){
channel = LogManager.slackProdChannel;
}else{
channel = LogManager.slackDevChannel;
}
LogManager.slack.webhook({
channel: channel,
username: "server",
text: text
}, function(err, response) {
//console.log(response);
});
};
module.exports = LogManager;