-
Notifications
You must be signed in to change notification settings - Fork 0
/
log-output.js
243 lines (197 loc) · 7.33 KB
/
log-output.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* eslint no-console: 0 */
const align = require('multipad');
const caller = require('callers-path');
const path = require('path');
const trucolor = require('trucolor');
// 24bit color chalk-style palette
const chalk = (0, trucolor.chalkish)((0, trucolor.palette)({}, {
black : 'rgb:48,48,48',
blue : 'rgb:51,152,219',
cyan : 'rgb:0,200,200',
green : 'rgb:47,223,100',
gray : 'rgb:144,144,144',
orange : 'rgb:255,153,50',
pink : 'rgb:178,0,140',
purple : 'rgb:114,83,178',
red : 'rgb:231,76,60',
white : 'rgb:224,224,224',
yellow : 'rgb:255,204,50',
lightgray : 'rgb:175,175,175',
boldblack : 'bold rgb:48,48,48',
boldblue : 'bold rgb:51,152,219',
boldcyan : 'bold rgb:0,200,200',
boldgreen : 'bold rgb:47,223,100',
boldgray : 'bold rgb:144,144,144',
boldorange : 'bold rgb:255,153,50',
boldpink : 'bold rgb:178,0,140',
boldpurple : 'bold rgb:114,83,178',
boldred : 'bold rgb:231,76,60',
boldwhite : 'bold rgb:224,224,224',
boldyellow : 'bold rgb:255,204,50',
italicblack : 'italic rgb:48,48,48',
italicblue : 'italic rgb:51,152,219',
italiccyan : 'italic rgb:0,200,200',
italicgreen : 'italic rgb:47,223,100',
italicgray : 'italic rgb:144,144,144',
italicorange : 'italic rgb:255,153,50',
italicpink : 'italic rgb:178,0,140',
italicpurple : 'italic rgb:114,83,178',
italicred : 'italic rgb:231,76,60',
italicwhite : 'italic rgb:224,224,224',
italicyellow : 'italic rgb:255,204,50',
}));
const padding = {
src : 6,
topic : 9,
};
function center(string, width) {
return align.center(string, width, ' ');
}
// Colorize data source string by name
function colorizeSource(sourceName) {
switch (sourceName.trim()) {
case 'api' : sourceName = chalk.green(sourceName); break;
case 'index' : sourceName = chalk.cyan(sourceName); break;
case 'mqtt' : sourceName = chalk.purple(sourceName); break;
case 'xbee' : sourceName = chalk.orange(sourceName); break;
default : sourceName = chalk.yellow(sourceName);
}
return sourceName;
}
function colorize(string) {
string = string.toString();
string = string.replace('Attempting', chalk.yellow('Attempting'));
string = string.replace('Connecting', chalk.yellow('Connecting'));
string = string.replace('Initializing', chalk.yellow('Initializing'));
string = string.replace('Reset', chalk.yellow('Reset'));
string = string.replace('Shutting down', chalk.yellow('Shutting down'));
string = string.replace('Starting', chalk.yellow('Starting'));
string = string.replace('Stopping', chalk.yellow('Stopping'));
string = string.replace('Terminating', chalk.yellow('Terminating'));
string = string.replace('Disconnected', chalk.red('Disconnected'));
string = string.replace('Error', chalk.red('Error'));
string = string.replace('SIGINT', chalk.red('SIGINT'));
string = string.replace('SIGTERM', chalk.red('SIGTERM'));
string = string.replace('Shut down', chalk.red('Shut down'));
string = string.replace('Stopped', chalk.red('Stopped'));
string = string.replace('Terminated', chalk.red('Terminated'));
string = string.replace('Unset', chalk.red('Unset'));
string = string.replace(' closed', chalk.red(' closed'));
string = string.replace(' disconnected', chalk.red(' disconnected'));
string = string.replace('error', chalk.red('error'));
string = string.replace('false', chalk.red('false'));
string = string.replace('Connected ', chalk.green('Connected '));
string = string.replace('Reconnected ', chalk.green('Reconnected '));
string = string.replace('reconnected ', chalk.green('reconnected '));
string = string.replace('Initialized', chalk.green('Initialized'));
string = string.replace('Listening ', chalk.green('Listening '));
string = string.replace('Loaded ', chalk.green('Loaded '));
string = string.replace('Read ', chalk.green('Read '));
string = string.replace('Set ', chalk.green('Set '));
string = string.replace('Started', chalk.green('Started'));
string = string.replace('Wrote', chalk.green('Wrote'));
string = string.replace(' connected', chalk.green(' connected'));
string = string.replace(' opened', chalk.green(' opened'));
string = string.replace('true', chalk.green('true'));
return string;
}
// Formatted output for when a value changes
function change(data) {
let dataNew = data.valueNew;
let dataSrc = path.parse(caller()).name;
let dataTopic = 'CHANGE';
let dataValue = data.keyFull;
// Pad strings
dataSrc = center(dataSrc, padding.src);
dataTopic = center(dataTopic, padding.topic);
// Catch nulls
if (typeof data.valueNew === 'undefined' || data.valueNew === null) data.valueNew = 'null';
// Colorize strings
dataSrc = chalk.blue(dataSrc);
dataTopic = chalk.cyan(dataTopic);
dataValue = chalk.boldblue(dataValue);
// Replace and colorize true/false
let dataNewFormat;
switch (typeof dataNew) {
case 'boolean' : {
dataNew = dataNew.toString().replace('true', chalk.green('true')).replace('false', chalk.red('false'));
dataNewFormat = '%s';
break;
}
case 'string' : {
dataNew = chalk.cyan(dataNew);
dataNewFormat = '\'%s\'';
break;
}
default : {
dataNewFormat = '%o';
}
}
// Output formatted string
console.log('[%s] [%s] %s: ' + dataNewFormat, dataSrc, dataTopic, dataValue, dataNew);
} // change(data)
function error(dataMsg, ...rest) {
let dataSrc = path.parse(caller()).name;
let dataTopic = 'ERROR';
// Pad strings
dataSrc = center(dataSrc, padding.src);
dataTopic = center(dataTopic, padding.topic);
// Colorize strings
dataSrc = chalk.red(dataSrc);
dataTopic = chalk.red(dataTopic);
// Only colorize log message if it is a string
let dataMsgFormat = '%o';
if (typeof dataMsg === 'string') {
dataMsgFormat = '%s';
}
// Output formatted string
console.log('[%s] [%s] ' + dataMsgFormat, dataSrc, dataTopic, dataMsg, ...rest);
} // error(data)
function lib(dataMsg, ...rest) {
if (appEnv === 'production') return;
let dataSrc = path.parse(caller()).name;
let dataTopic = 'LIBRARY';
// Pad strings
dataSrc = center(dataSrc, padding.src);
dataTopic = center(dataTopic, padding.topic);
// Colorize strings
dataSrc = colorizeSource(dataSrc);
dataTopic = chalk.gray(dataTopic);
// Only colorize log message if it is a string
let dataMsgFormat = '%o';
if (typeof dataMsg === 'string') {
dataMsg = chalk.lightgray(dataMsg);
dataMsg = colorize(dataMsg);
dataMsgFormat = '%s';
}
// Output formatted string
console.log('[%s] [%s] ' + dataMsgFormat, dataSrc, dataTopic, dataMsg, ...rest);
} // lib(data)
function msg(dataMsg, ...rest) {
if (appEnv === 'production') return;
let dataSrc = path.parse(caller()).name;
let dataTopic = 'MESSAGE';
// Pad strings
dataSrc = center(dataSrc, padding.src);
dataTopic = center(dataTopic, padding.topic);
// Colorize strings
dataSrc = colorizeSource(dataSrc);
dataTopic = chalk.pink(dataTopic);
// Only colorize log message if it is a string
let dataMsgFormat = '%o';
if (typeof dataMsg === 'string') {
dataMsg = chalk.italicpink(dataMsg);
dataMsg = colorize(dataMsg);
dataMsgFormat = '%s';
}
// Output formatted string
console.log('[%s] [%s] ' + dataMsgFormat, dataSrc, dataTopic, dataMsg, ...rest);
} // msg(data)
module.exports = {
// 24bit color chalk-style palette
chalk,
change,
error,
lib,
msg,
};