This repository has been archived by the owner on Mar 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMMM-syslog.js
120 lines (102 loc) · 3.36 KB
/
MMM-syslog.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
/* global Module */
/* Magic Mirror
* Module: MMM-syslog
*
* By Paul-Vincent Roll http://paulvincentroll.com
* MIT Licensed.
*/
Module.register('MMM-syslog',{
messages: [],
defaults: {
max: 5,
format: false,
types: {
INFO: "dimmed",
WARNING: "normal",
ERROR: "bright"
},
icons: {
INFO: "info",
WARNING: "exclamation",
ERROR: "exclamation-triangle"
},
shortenMessage: false,
alert: true
},
getStyles: function () {
return ["font-awesome.css"];
},
getScripts: function() {
return ["moment.js"];
},
start: function() {
this.sendSocketNotification("CONNECT", {max: this.config.max, logFile: this.file('logs.json')});
Log.info("Starting module: " + this.name);
moment.locale(config.language);
//Update DOM every minute so that the time of the call updates and calls get removed after a certain time
setInterval(() => {
this.updateDom();
}, 60000);
},
socketNotificationReceived: function(notification, payload) {
if(notification === "NEW_MESSAGE"){
if (this.config.alert && !payload.silent) {
this.sendNotification("SHOW_ALERT", {type: "notification", title: payload.type, message: payload.message});
}
this.messages.push(payload);
while(this.messages.length > this.config.max){
this.messages.shift();
}
this.updateDom(3000);
}
},
getDom: function() {
var wrapper = document.createElement("div");
if(this.config.title !== false){
var title = document.createElement("header");
title.innerHTML = this.config.title || this.name;
wrapper.appendChild(title);
}
var logs = document.createElement("table");
for (var i = this.messages.length - 1; i >= 0; i--) {
//Create callWrapper
var callWrapper = document.createElement("tr");
callWrapper.classList.add("normal");
var iconCell = document.createElement("td");
var icon = document.createElement("i");
if(this.config.icons.hasOwnProperty(this.messages[i].type)){
icon.classList.add("fa", "fa-fw", "fa-" + this.config.icons[this.messages[i].type]);
}
else {
icon.classList.add("fa", "fa-fw", "fa-question");
}
if(this.config.types.hasOwnProperty(this.messages[i].type)){
icon.classList.add(this.config.types[this.messages[i].type]);
}
iconCell.classList.add("small");
iconCell.appendChild(icon);
callWrapper.appendChild(iconCell);
var message = this.messages[i].message;
if(this.config.shortenMessage && message.length > this.config.shortenMessage){
message = message.slice(0, this.config.shortenMessage) + "…";
}
//Set caller of row
var caller = document.createElement("td");
caller.innerHTML = " " + message;
caller.classList.add("title", "small", "align-left");
if(this.config.types.hasOwnProperty(this.messages[i].type)){
caller.classList.add(this.config.types[this.messages[i].type]);
}
callWrapper.appendChild(caller);
//Set time of row
var time = document.createElement("td");
time.innerHTML = this.config.format ? moment(this.messages[i].timestamp).format(this.config.format) : moment(this.messages[i].timestamp).fromNow();
time.classList.add("time", "light", "xsmall");
callWrapper.appendChild(time);
//Add to logs
logs.appendChild(callWrapper);
}
wrapper.appendChild(logs);
return wrapper;
}
});