-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggerPlus.js
208 lines (178 loc) · 6.29 KB
/
loggerPlus.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
const getCurrentTimestamp = () => {
const now = new Date();
return now.toTimeString().split(' ')[0]; // HH:MM:SS
};
const formatMessage = (msg) => {
if (msg === true || msg === false) {
return [String(msg)];
}
if (typeof msg === 'object') {
try {
return JSON.stringify(msg, null, 2).split('\n').map(line => line.trim());
} catch (e) {
return ['[Unable to stringify object]'];
}
}
return [msg || '']; // Ensure that msg is not undefined
};
const logBase = (type, emoji, message, screenOrFunction = '(not called)', ...messages) => {
const timestamp = getCurrentTimestamp();
// Ensure that screenOrFunction is a string; otherwise, treat it as a message
if (typeof screenOrFunction !== 'string') {
messages.unshift(screenOrFunction);
screenOrFunction = '(not called)';
}
console.group('');
console[type]('---------------------------------');
console[type](`${emoji} from ${screenOrFunction}`);
console[type]('---------------------------------');
console[type]('');
const lines = formatMessage(message);
console[type](lines[0]);
for (let i = 1; i < lines.length; i++) {
console.log(lines[i]);
}
// Handle messages properly, including logging boolean values like false/true
messages.forEach(msg => {
if (msg !== undefined) {
const msgLines = formatMessage(msg);
msgLines.forEach(line => console.log(line));
}
});
console.log(''); // Add a line break before the timestamp
console[type](`[${timestamp}]`);
console[type]('');
console.groupEnd();
};
const logError = (message, screenOrFunction = '(not called)', ...messages) => {
const timestamp = getCurrentTimestamp();
// Ensure that screenOrFunction is a string; otherwise, treat it as a message
if (typeof screenOrFunction !== 'string') {
messages.unshift(screenOrFunction);
screenOrFunction = '(not called)';
}
console.group('');
console.log('---------------------------------');
console.log(`❌ from ${screenOrFunction}`);
console.log('---------------------------------');
console.log('');
const lines = formatMessage(message);
console.error(lines[0]);
for (let i = 1; i < lines.length; i++) {
console.log(lines[i]);
}
messages.forEach(msg => {
if (msg !== undefined) {
const msgLines = formatMessage(msg);
msgLines.forEach(line => console.log(line));
}
});
console.log(''); // Add a line break before the timestamp
console.log(`[${timestamp}]`);
console.log('');
console.groupEnd();
};
const logWarn = (message, screenOrFunction = '(not called)', ...messages) => {
const timestamp = getCurrentTimestamp();
// Ensure that screenOrFunction is a string; otherwise, treat it as a message
if (typeof screenOrFunction !== 'string') {
messages.unshift(screenOrFunction);
screenOrFunction = '(not called)';
}
console.group('');
console.log('---------------------------------');
console.log(`⚠️ from ${screenOrFunction}`);
console.log('---------------------------------');
console.log('');
const lines = formatMessage(message);
console.warn(lines[0]);
for (let i = 1; i < lines.length; i++) {
console.log(lines[i]);
}
messages.forEach(msg => {
if (msg !== undefined) {
const msgLines = formatMessage(msg);
msgLines.forEach(line => console.log(line));
}
});
console.log(''); // Add a line break before the timestamp
console.log(`[${timestamp}]`);
console.log('');
console.groupEnd();
};
const line = (message, ...messages) => {
const timestamp = getCurrentTimestamp();
console.group('');
formatMessage(message).forEach(line => console.log(line));
messages.forEach(msg => {
if (msg !== undefined && msg !== '') {
formatMessage(msg).forEach(line => console.log(line));
}
});
console.log(''); // Add a line break before the timestamp
console.log(`[${timestamp}]`);
console.log('----------------------------------');
console.groupEnd();
};
const printBox = (...lines) => {
const timestamp = getCurrentTimestamp();
const timestampFormatted = `[${timestamp}]`;
// Convert objects to strings
lines = lines.map(line => {
if (typeof line === 'object') {
try {
return JSON.stringify(line, null, 2).split('\n').map(subLine => subLine.trim());
} catch (e) {
return '[Unable to stringify object]';
}
}
return line || ''; // Ensure that line is not undefined
}).flat();
console.group(''); // Add group
// Find the longest line length
const maxLength = Math.max(...lines.map(line => line.length), timestampFormatted.length);
// Create the top border
let topBorder = '╔' + '═'.repeat(maxLength + 2) + '╗';
console.log(topBorder);
// Add a line spacer at the top
let lineSpacer = '║' + ' '.repeat(maxLength + 2) + '║';
console.log(lineSpacer);
// Create each line with padding
lines.forEach(line => {
console.log(lineSpacer);
let padding = ' '.repeat(maxLength - line.length);
console.log(`║ ${line}${padding} ║`);
});
// Add the timestamp
console.log(lineSpacer);
let timestampPadding = ' '.repeat(maxLength - timestampFormatted.length);
console.log(`║ ${timestampFormatted}${timestampPadding} ║`);
console.log(lineSpacer);
// Create the bottom border
let bottomBorder = '╚' + '═'.repeat(maxLength + 2) + '╝';
console.log(bottomBorder);
console.groupEnd(); // End group
};
const log = (message, screenOrFunction = '(not called)', ...messages) => logBase('log', '', message, screenOrFunction, ...messages);
const success = (message, screenOrFunction = '(not called)', ...messages) => logBase('log', '✅', message, screenOrFunction, ...messages);
const info = (message, screenOrFunction = '(not called)', ...messages) => logBase('info', 'ℹ️', message, screenOrFunction, ...messages);
const warn = (message, screenOrFunction = '(not called)', ...messages) => logWarn(message, screenOrFunction, ...messages);
const error = (message, screenOrFunction = '(not called)', ...messages) => logError(message, screenOrFunction, ...messages);
const box = (...lines) => {
printBox(...lines);
};
const Logger = {
log,
success,
info,
warn,
error,
line,
box,
};
// UMD pattern to support both CommonJS and ES Modules
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Logger;
} else {
window.Logger = Logger;
}