slicejs-web-framework 3.3.7 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,145 +1,225 @@
1
- import Log from './Log.js';
2
-
3
- export default class Logger {
4
- constructor() {
5
- this.logs = [];
6
- this.logEnabled = slice.loggerConfig.enabled;
7
- this.showLogsConfig = slice.loggerConfig.showLogs;
8
-
9
- this.showLog = function showLog(log) {
10
- if (!this.showLogsConfig) return;
11
-
12
- const logType = log.logType;
13
-
14
- Object.keys(this.showLogsConfig).forEach((logConfig) => {
15
- if (this.showLogsConfig[logConfig][logType] === true) {
16
- switch (logConfig) {
17
- case 'console':
18
- switch (logType) {
19
- case logTypes.ERROR:
20
- console.error(
21
- `\x1b[31mERROR\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message} - ${log.error}`
22
- );
23
- break;
24
- case logTypes.WARNING:
25
- console.warn(
26
- `\x1b[33m⚠ WARNING\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`
27
- );
28
- break;
29
- case logTypes.INFO:
30
- console.log(
31
- `\x1b[32m✔ INFO\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`
32
- );
33
- break;
34
- default:
35
- console.log(
36
- `\x1b[37mUNKNOWN\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`
37
- );
38
- }
39
- break;
40
- }
41
- }
42
- });
43
- };
44
- }
45
-
46
- createLog(logType, componentSliceId, message, error = null) {
47
- if (!this.logEnabled) return;
48
- let componentName;
49
-
50
- try {
51
- componentName = slice.controller.activeComponents.get(componentSliceId).constructor.name;
52
- } catch (error) {
53
- componentName = componentSliceId;
54
- }
55
-
56
- let componentCategory = slice.controller.getComponentCategory(componentName);
57
- if (componentSliceId === 'Slice' || componentSliceId === 'ThemeManager') componentCategory = 'Structural';
58
- const log = new Log(logType, componentCategory, componentSliceId, message, error);
59
- this.logs.push(log);
60
- this.showLog(log);
61
- }
62
-
63
- /**
64
- * Log an error message.
65
- * @param {string} componentSliceId
66
- * @param {string} message
67
- * @param {any} [error]
68
- * @returns {void}
69
- */
70
- logError(componentSliceId, message, error) {
71
- this.createLog(logTypes.ERROR, componentSliceId, message, error);
72
- }
73
-
74
- /**
75
- * Log a warning message.
76
- * @param {string} componentSliceId
77
- * @param {string} message
78
- * @returns {void}
79
- */
80
- logWarning(componentSliceId, message) {
81
- this.createLog(logTypes.WARNING, componentSliceId, message);
82
- }
83
-
84
- /**
85
- * Log an info message.
86
- * @param {string} componentSliceId
87
- * @param {string} message
88
- * @returns {void}
89
- */
90
- logInfo(componentSliceId, message) {
91
- this.createLog(logTypes.INFO, componentSliceId, message);
92
- }
93
-
94
- /**
95
- * Get all logs.
96
- * @returns {Array}
97
- */
98
- getLogs() {
99
- return this.logs;
100
- }
101
-
102
- /**
103
- * Clear all logs.
104
- * @returns {void}
105
- */
106
- clearLogs() {
107
- this.logs = [];
108
- }
109
-
110
- /**
111
- * Filter logs by type.
112
- * @param {string} type
113
- * @returns {Array}
114
- */
115
- getLogsByLogType(type) {
116
- return this.logs.filter((log) => log.logType === type);
117
- }
118
-
119
- /**
120
- * Filter logs by component category.
121
- * @param {string} componentCategory
122
- * @returns {Array}
123
- */
124
- getLogsByComponentCategory(componentCategory) {
125
- return this.logs.filter((log) => log.componentCategory === componentCategory);
126
- }
127
-
128
- /**
129
- * Filter logs by component sliceId.
130
- * @param {string} componentSliceId
131
- * @returns {Array}
132
- */
133
- getLogsByComponent(componentSliceId) {
134
- return this.logs.filter((log) => log.componentSliceId === componentSliceId);
135
- }
136
- }
137
-
138
- // En esta misma idea, se tiene que tomar en cuenta que el componente de ToastAlert será un toastProvider y que solo debe
139
- // haber un toastProvider en la página, por lo que se debe implementar un Singleton para el ToastProvider
140
-
141
- const logTypes = {
142
- ERROR: 'error',
143
- WARNING: 'warning',
144
- INFO: 'info',
145
- };
1
+ import Log from './Log.js';
2
+
3
+ const logTypes = {
4
+ ERROR: 'error',
5
+ WARN: 'warn',
6
+ INFO: 'info',
7
+ DEBUG: 'debug',
8
+ };
9
+
10
+ export default class Logger {
11
+ constructor() {
12
+ this.logs = [];
13
+ this._logListeners = new Set();
14
+ this.logEnabled = slice.loggerConfig.enabled;
15
+ this.showLogsConfig = slice.loggerConfig.showLogs;
16
+
17
+ this.showLog = function showLog(log) {
18
+ if (!this.showLogsConfig) return;
19
+
20
+ const logType = log.logType;
21
+
22
+ Object.keys(this.showLogsConfig).forEach((logConfig) => {
23
+ if (this.showLogsConfig[logConfig][logType] === true) {
24
+ switch (logConfig) {
25
+ case 'console':
26
+ switch (logType) {
27
+ case logTypes.ERROR:
28
+ console.error(
29
+ `\x1b[31mERROR\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`,
30
+ log.error
31
+ );
32
+ break;
33
+ case logTypes.WARN:
34
+ if (log.error) {
35
+ console.warn(
36
+ `\x1b[33m⚠ WARN\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`,
37
+ log.error
38
+ );
39
+ } else {
40
+ console.warn(
41
+ `\x1b[33m⚠ WARN\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`
42
+ );
43
+ }
44
+ break;
45
+ case logTypes.INFO:
46
+ if (log.error) {
47
+ console.log(
48
+ `\x1b[32m✔ INFO\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`,
49
+ log.error
50
+ );
51
+ } else {
52
+ console.log(
53
+ `\x1b[32m✔ INFO\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`
54
+ );
55
+ }
56
+ break;
57
+ case logTypes.DEBUG:
58
+ if (log.error) {
59
+ console.debug(
60
+ `\x1b[36m🔍 DEBUG\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`,
61
+ log.error
62
+ );
63
+ } else {
64
+ console.debug(
65
+ `\x1b[36m🔍 DEBUG\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`
66
+ );
67
+ }
68
+ break;
69
+ default:
70
+ console.log(
71
+ `\x1b[37mUNKNOWN\x1b[0m - ${log.componentCategory} - ${log.componentSliceId} - ${log.message}`
72
+ );
73
+ }
74
+ break;
75
+ }
76
+ }
77
+ });
78
+ };
79
+ }
80
+
81
+ createLog(logType, componentSliceId, message, error = null) {
82
+ if (!this.logEnabled) return;
83
+ let componentName;
84
+
85
+ try {
86
+ componentName = slice.controller.activeComponents.get(componentSliceId).constructor.name;
87
+ } catch (_err) {
88
+ componentName = componentSliceId;
89
+ }
90
+
91
+ let componentCategory = slice.controller.getComponentCategory(componentName);
92
+ if (componentSliceId === 'Slice' || componentSliceId === 'ThemeManager') componentCategory = 'Structural';
93
+ const log = new Log(logType, componentCategory, componentSliceId, message, error);
94
+ this.logs.push(log);
95
+ this.showLog(log);
96
+ for (const listener of this._logListeners) {
97
+ try { listener(log); } catch (_) { }
98
+ }
99
+ }
100
+
101
+ /**
102
+ * Log an error message.
103
+ * @param {string} componentSliceId
104
+ * @param {string} message
105
+ * @param {any} [error]
106
+ * @returns {void}
107
+ */
108
+ error(componentSliceId, message, error) {
109
+ this.createLog(logTypes.ERROR, componentSliceId, message, error);
110
+ }
111
+
112
+ /**
113
+ * Log a warning message.
114
+ * @param {string} componentSliceId
115
+ * @param {string} message
116
+ * @param {any} [error]
117
+ * @returns {void}
118
+ */
119
+ warn(componentSliceId, message, error) {
120
+ this.createLog(logTypes.WARN, componentSliceId, message, error);
121
+ }
122
+
123
+ /**
124
+ * Log an info message.
125
+ * @param {string} componentSliceId
126
+ * @param {string} message
127
+ * @param {any} [error]
128
+ * @returns {void}
129
+ */
130
+ info(componentSliceId, message, error) {
131
+ this.createLog(logTypes.INFO, componentSliceId, message, error);
132
+ }
133
+
134
+ /**
135
+ * Log a debug message.
136
+ * @param {string} componentSliceId
137
+ * @param {string} message
138
+ * @param {any} [error]
139
+ * @returns {void}
140
+ */
141
+ debug(componentSliceId, message, error) {
142
+ this.createLog(logTypes.DEBUG, componentSliceId, message, error);
143
+ }
144
+
145
+ // ── Retrocompatibilidad: métodos antiguos redirigen a los nuevos ──
146
+
147
+ /** @deprecated Use .error() instead */
148
+ logError(componentSliceId, message, error) {
149
+ this.error(componentSliceId, message, error);
150
+ }
151
+
152
+ /** @deprecated Use .warn() instead */
153
+ logWarning(componentSliceId, message, error) {
154
+ this.warn(componentSliceId, message, error);
155
+ }
156
+
157
+ /** @deprecated Use .info() instead */
158
+ logInfo(componentSliceId, message, error) {
159
+ this.info(componentSliceId, message, error);
160
+ }
161
+
162
+ /**
163
+ * Subscribe to new log entries in real time.
164
+ * @param {Function} callback — receives the Log instance
165
+ * @returns {Function} the same callback (for passing to offLog)
166
+ */
167
+ onLog(callback) {
168
+ if (typeof callback === 'function') {
169
+ this._logListeners.add(callback);
170
+ }
171
+ return callback;
172
+ }
173
+
174
+ /**
175
+ * Unsubscribe a previously registered listener.
176
+ * @param {Function} callback
177
+ * @returns {void}
178
+ */
179
+ offLog(callback) {
180
+ this._logListeners.delete(callback);
181
+ }
182
+
183
+ /**
184
+ * Get all logs.
185
+ * @returns {Array}
186
+ */
187
+ getLogs() {
188
+ return this.logs;
189
+ }
190
+
191
+ /**
192
+ * Clear all logs.
193
+ * @returns {void}
194
+ */
195
+ clearLogs() {
196
+ this.logs = [];
197
+ }
198
+
199
+ /**
200
+ * Filter logs by type.
201
+ * @param {string} type
202
+ * @returns {Array}
203
+ */
204
+ getLogsByLogType(type) {
205
+ return this.logs.filter((log) => log.logType === type);
206
+ }
207
+
208
+ /**
209
+ * Filter logs by component category.
210
+ * @param {string} componentCategory
211
+ * @returns {Array}
212
+ */
213
+ getLogsByComponentCategory(componentCategory) {
214
+ return this.logs.filter((log) => log.componentCategory === componentCategory);
215
+ }
216
+
217
+ /**
218
+ * Filter logs by component sliceId.
219
+ * @param {string} componentSliceId
220
+ * @returns {Array}
221
+ */
222
+ getLogsByComponent(componentSliceId) {
223
+ return this.logs.filter((log) => log.componentSliceId === componentSliceId);
224
+ }
225
+ }