@sixbell-telco/sdk 1.3.0 → 1.5.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.
- package/CHANGELOG.md +81 -0
- package/README.md +437 -472
- package/_index.css +109 -11
- package/assets/styles/toast.css +8 -0
- package/components/forms/date-picker/constants/date-picker.constants.d.ts +13 -0
- package/components/forms/date-picker/index.d.ts +5 -0
- package/components/forms/date-picker/public-api.d.ts +3 -0
- package/components/forms/date-picker/src/date-picker.component.d.ts +323 -0
- package/components/forms/date-picker/types/date-picker.types.d.ts +39 -0
- package/components/{forms/datepicker → sonner}/index.d.ts +1 -1
- package/components/sonner/public-api.d.ts +2 -0
- package/components/sonner/src/sonner.component.d.ts +39 -0
- package/components/sonner/toaster/index.d.ts +5 -0
- package/components/sonner/toaster/public-api.d.ts +1 -0
- package/components/sonner/toaster/src/toaster.component.d.ts +89 -0
- package/components/toast/public-api.d.ts +9 -0
- package/components/toast/services/toast.service.d.ts +3 -0
- package/components/toast/toast-message/toast-message.component.d.ts +3 -0
- package/components/toast/toasts-container/toasts-container.component.d.ts +3 -0
- package/fesm2022/sixbell-telco-sdk-components-forms-date-picker.mjs +885 -0
- package/fesm2022/sixbell-telco-sdk-components-forms-date-picker.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner-toaster.mjs +132 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner-toaster.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner.mjs +179 -0
- package/fesm2022/sixbell-telco-sdk-components-sonner.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-components-toast.mjs +14 -0
- package/fesm2022/sixbell-telco-sdk-components-toast.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-date.mjs +90 -2
- package/fesm2022/sixbell-telco-sdk-utils-date.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-logger.mjs +578 -0
- package/fesm2022/sixbell-telco-sdk-utils-logger.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-utils-resource-loader.mjs +1222 -0
- package/fesm2022/sixbell-telco-sdk-utils-resource-loader.mjs.map +1 -0
- package/fesm2022/sixbell-telco-sdk-utils-theme.mjs +1434 -180
- package/fesm2022/sixbell-telco-sdk-utils-theme.mjs.map +1 -1
- package/fesm2022/sixbell-telco-sdk-utils-translation.mjs +734 -182
- package/fesm2022/sixbell-telco-sdk-utils-translation.mjs.map +1 -1
- package/package.json +68 -51
- package/utils/date/src/date-utils.d.ts +44 -0
- package/utils/logger/README.md +539 -0
- package/utils/logger/index.d.ts +5 -0
- package/utils/logger/public-api.d.ts +3 -0
- package/utils/logger/src/constants.d.ts +15 -0
- package/utils/logger/src/logger.service.d.ts +123 -0
- package/utils/logger/src/models/logger.d.ts +25 -0
- package/utils/logger/src/providers/logger.provider.d.ts +3 -0
- package/utils/resource-loader/README.md +460 -0
- package/utils/resource-loader/index.d.ts +5 -0
- package/utils/resource-loader/public-api.d.ts +1 -0
- package/utils/resource-loader/src/cache/index.d.ts +2 -0
- package/utils/resource-loader/src/cache/layers/factory.d.ts +38 -0
- package/utils/resource-loader/src/cache/layers/index.d.ts +3 -0
- package/utils/resource-loader/src/cache/layers/indexed-db-cache.d.ts +118 -0
- package/utils/resource-loader/src/cache/layers/local-storage-cache.d.ts +86 -0
- package/utils/resource-loader/src/cache/layers/memory-cache.d.ts +76 -0
- package/utils/resource-loader/src/cache/types.d.ts +107 -0
- package/utils/resource-loader/src/config/index.d.ts +1 -0
- package/utils/resource-loader/src/config/load-config.d.ts +32 -0
- package/utils/resource-loader/src/constants.d.ts +38 -0
- package/utils/resource-loader/src/http/index.d.ts +1 -0
- package/utils/resource-loader/src/http/retryable-client.d.ts +43 -0
- package/utils/resource-loader/src/index.d.ts +1 -0
- package/utils/resource-loader/src/resource-loader.d.ts +67 -0
- package/utils/resource-loader/src/types.d.ts +152 -0
- package/utils/resource-loader/src/utils/cache-validator.d.ts +72 -0
- package/utils/resource-loader/src/utils/helpers.d.ts +15 -0
- package/utils/resource-loader/src/utils/index.d.ts +2 -0
- package/utils/theme/README.md +1054 -109
- package/utils/theme/public-api.d.ts +9 -1
- package/utils/theme/src/constants.d.ts +74 -0
- package/utils/theme/src/models/theme.d.ts +288 -0
- package/utils/theme/src/providers/constants.d.ts +7 -0
- package/utils/theme/src/providers/theme.provider.d.ts +13 -0
- package/utils/theme/src/providers/theme.provider.types.d.ts +29 -0
- package/utils/theme/src/theme-assets/theme-assets.service.d.ts +214 -0
- package/utils/theme/src/theme-assets/theme-assets.types.d.ts +10 -0
- package/utils/theme/src/theme-dom/theme-dom.service.d.ts +87 -0
- package/utils/theme/src/theme-scheme/theme-scheme.service.d.ts +44 -0
- package/utils/theme/src/theme-storage/theme-storage.service.d.ts +47 -0
- package/utils/theme/src/theme.service.d.ts +40 -186
- package/utils/theme/src/utils/utils.d.ts +86 -0
- package/utils/translation/README.md +1414 -176
- package/utils/translation/public-api.d.ts +6 -0
- package/utils/translation/src/constants.d.ts +52 -0
- package/utils/translation/src/models/translation.d.ts +59 -0
- package/utils/translation/src/providers/constants.d.ts +11 -0
- package/utils/translation/src/providers/translation.provider.d.ts +73 -0
- package/utils/translation/src/providers/translation.provider.types.d.ts +26 -0
- package/utils/translation/src/translation-dom/translation-dom.service.d.ts +26 -0
- package/utils/translation/src/translation-storage/translation-storage.service.d.ts +34 -0
- package/utils/translation/src/translation.service.d.ts +19 -166
- package/components/forms/datepicker/constants/datepicker.constants.d.ts +0 -5
- package/components/forms/datepicker/public-api.d.ts +0 -3
- package/components/forms/datepicker/src/datepicker.component.d.ts +0 -165
- package/components/forms/datepicker/types/datepicker.types.d.ts +0 -7
- package/fesm2022/sixbell-telco-sdk-components-forms-datepicker.mjs +0 -487
- package/fesm2022/sixbell-telco-sdk-components-forms-datepicker.mjs.map +0 -1
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
# Logger Utility
|
|
2
|
+
|
|
3
|
+
A comprehensive, theme-aware logging service for the Sixbell Telco SDK with support for multiple log levels, console output, in-memory storage, and persistent IndexedDB logging.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 📊 **6 Log Levels**: trace, debug, info, warn, error, fatal
|
|
8
|
+
- 🎨 **Theme-Aware**: Automatic dark/light mode color schemes
|
|
9
|
+
- 💾 **Persistent Storage**: Optional IndexedDB logging with retention policies
|
|
10
|
+
- 🔇 **Silent Mode**: Log without console output
|
|
11
|
+
- 📝 **Context Support**: Add component, action, and custom metadata to logs
|
|
12
|
+
- 🔍 **Flexible Filtering**: Log level thresholds to control output
|
|
13
|
+
- 🏭 **Factory Pattern**: DI-ready provider for easy Angular integration
|
|
14
|
+
- 📥 **Log Download**: Export logs as JSON files
|
|
15
|
+
- 🧪 **Fully Tested**: 50+ tests covering all functionality
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
The logger is included in the UI kit. Import from:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { LoggerService } from '@sixbell-telco/sdk/utils/logger';
|
|
23
|
+
import { provideLogger } from '@sixbell-telco/sdk/utils/logger';
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
### Basic Usage
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Component, inject } from '@angular/core';
|
|
32
|
+
import { LoggerService } from '@sixbell-telco/sdk/utils/logger';
|
|
33
|
+
|
|
34
|
+
@Component({
|
|
35
|
+
selector: 'app-example',
|
|
36
|
+
template: `<div>Check console for logs</div>`,
|
|
37
|
+
})
|
|
38
|
+
export class ExampleComponent {
|
|
39
|
+
private logger = inject(LoggerService);
|
|
40
|
+
|
|
41
|
+
ngOnInit() {
|
|
42
|
+
this.logger.info('Component initialized');
|
|
43
|
+
this.logger.warn('This is a warning');
|
|
44
|
+
this.logger.error('Something went wrong', new Error('Details here'));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Setup in Application
|
|
50
|
+
|
|
51
|
+
Configure the logger in your application bootstrap:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { bootstrapApplication } from '@angular/platform-browser';
|
|
55
|
+
import { provideLogger } from '@sixbell-telco/sdk/utils/logger';
|
|
56
|
+
import { AppComponent } from './app.component';
|
|
57
|
+
|
|
58
|
+
bootstrapApplication(AppComponent, {
|
|
59
|
+
providers: [
|
|
60
|
+
provideLogger({
|
|
61
|
+
enableLogging: true,
|
|
62
|
+
logLevel: 'debug',
|
|
63
|
+
showTimestamp: true,
|
|
64
|
+
isDarkMode: false,
|
|
65
|
+
}),
|
|
66
|
+
],
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Or in NgModule:
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
import { NgModule } from '@angular/core';
|
|
74
|
+
import { provideLogger } from '@sixbell-telco/sdk/utils/logger';
|
|
75
|
+
|
|
76
|
+
@NgModule({
|
|
77
|
+
providers: [
|
|
78
|
+
provideLogger({
|
|
79
|
+
enableLogging: true,
|
|
80
|
+
logLevel: 'info',
|
|
81
|
+
}),
|
|
82
|
+
],
|
|
83
|
+
})
|
|
84
|
+
export class AppModule {}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Log Levels
|
|
88
|
+
|
|
89
|
+
Log levels are ordered by severity. The logger will only output messages at or above the configured threshold.
|
|
90
|
+
|
|
91
|
+
| Level | Value | Use Case | Color |
|
|
92
|
+
| --------- | ----- | ----------------------------------- | ------------ |
|
|
93
|
+
| **trace** | 0 | Very detailed debugging information | Gray |
|
|
94
|
+
| **debug** | 1 | General debugging information | Blue |
|
|
95
|
+
| **info** | 2 | Informational messages | Green |
|
|
96
|
+
| **warn** | 3 | Warning conditions | Orange |
|
|
97
|
+
| **error** | 4 | Error conditions | Red |
|
|
98
|
+
| **fatal** | 5 | Fatal errors that may crash the app | White on Red |
|
|
99
|
+
|
|
100
|
+
### Example: Log Level Filtering
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
// With logLevel: 'warn', only warn, error, and fatal will appear
|
|
104
|
+
logger.configure({ logLevel: 'warn' });
|
|
105
|
+
|
|
106
|
+
logger.debug('Not shown'); // ✗ Below threshold
|
|
107
|
+
logger.info('Not shown'); // ✗ Below threshold
|
|
108
|
+
logger.warn('Will show'); // ✓ At threshold
|
|
109
|
+
logger.error('Will show'); // ✓ Above threshold
|
|
110
|
+
logger.fatal('Will show'); // ✓ Above threshold
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## API Reference
|
|
114
|
+
|
|
115
|
+
### Configuration
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
interface LoggerConfig {
|
|
119
|
+
enableLogging?: boolean; // Enable/disable logging (default: false)
|
|
120
|
+
logLevel?: LogLevel; // Minimum level to log (default: 'error')
|
|
121
|
+
showTimestamp?: boolean; // Show time in logs (default: false)
|
|
122
|
+
showDate?: boolean; // Show date + time (default: false)
|
|
123
|
+
silentMode?: boolean; // Log without console output (default: false)
|
|
124
|
+
persistToIndexedDB?: boolean; // Save logs to IndexedDB (default: false)
|
|
125
|
+
idbRetentionDays?: number; // Keep logs for X days (default: 7)
|
|
126
|
+
idbMaxLogs?: number; // Max logs in IndexedDB (default: 5000)
|
|
127
|
+
isDarkMode?: boolean; // Use dark theme colors (default: false)
|
|
128
|
+
onError?: (error, context) => void; // Error callback
|
|
129
|
+
errorReporter?: { captureException }; // External error reporter
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Methods
|
|
134
|
+
|
|
135
|
+
#### Logging Methods
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
logger.trace(message: string, context?: LoggerContext)
|
|
139
|
+
logger.debug(message: string, context?: LoggerContext)
|
|
140
|
+
logger.info(message: string, context?: LoggerContext)
|
|
141
|
+
logger.warn(message: string, context?: LoggerContext)
|
|
142
|
+
logger.error(message: string, error?: Error, context?: LoggerContext)
|
|
143
|
+
logger.fatal(message: string, error?: Error, context?: LoggerContext)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### Configuration
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
// Configure globally
|
|
150
|
+
logger.configure({ enableLogging: true, logLevel: 'debug' });
|
|
151
|
+
|
|
152
|
+
// Create temporary logger with different config
|
|
153
|
+
const debugLogger = logger.withConfig({ logLevel: 'trace' });
|
|
154
|
+
debugLogger.debug('Detailed debug info');
|
|
155
|
+
logger.debug('Respects original config');
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### Storage & Retrieval
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
// Get in-memory logs
|
|
162
|
+
const logs = logger.getStoredLogs();
|
|
163
|
+
|
|
164
|
+
// Clear in-memory logs
|
|
165
|
+
logger.clearStoredLogs();
|
|
166
|
+
|
|
167
|
+
// Get persisted logs from IndexedDB
|
|
168
|
+
const persistedLogs = await logger.getPersistedLogs();
|
|
169
|
+
|
|
170
|
+
// Clear IndexedDB logs
|
|
171
|
+
await logger.clearPersistedLogs();
|
|
172
|
+
|
|
173
|
+
// Download all logs as JSON
|
|
174
|
+
await logger.downloadLogs('my-logs.json');
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
#### Theme Management
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
// Set dark mode explicitly
|
|
181
|
+
logger.setDarkMode(true);
|
|
182
|
+
|
|
183
|
+
// Get current theme
|
|
184
|
+
const isDark = logger.isDarkMode();
|
|
185
|
+
|
|
186
|
+
// Restore system preference detection
|
|
187
|
+
logger.setDarkMode(undefined);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
## Usage Examples
|
|
191
|
+
|
|
192
|
+
### Example 1: Basic Logging with Context
|
|
193
|
+
|
|
194
|
+
```typescript
|
|
195
|
+
import { Component, inject } from '@angular/core';
|
|
196
|
+
import { LoggerService } from '@sixbell-telco/sdk/utils/logger';
|
|
197
|
+
|
|
198
|
+
@Component({
|
|
199
|
+
selector: 'app-user-list',
|
|
200
|
+
template: `<div>Users</div>`,
|
|
201
|
+
})
|
|
202
|
+
export class UserListComponent {
|
|
203
|
+
private logger = inject(LoggerService);
|
|
204
|
+
|
|
205
|
+
loadUsers() {
|
|
206
|
+
const context = {
|
|
207
|
+
component: 'UserListComponent',
|
|
208
|
+
action: 'loadUsers',
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
this.logger.info('Loading users', context);
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
// Load users...
|
|
215
|
+
this.logger.info('Users loaded successfully', context);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
this.logger.error('Failed to load users', error as Error, context);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### Example 2: Environment-Specific Configuration
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
// environment.ts
|
|
227
|
+
export const environment = {
|
|
228
|
+
production: false,
|
|
229
|
+
logger: {
|
|
230
|
+
enableLogging: true,
|
|
231
|
+
logLevel: 'debug',
|
|
232
|
+
showTimestamp: true,
|
|
233
|
+
persistToIndexedDB: false,
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// environment.prod.ts
|
|
238
|
+
export const environment = {
|
|
239
|
+
production: true,
|
|
240
|
+
logger: {
|
|
241
|
+
enableLogging: true,
|
|
242
|
+
logLevel: 'warn',
|
|
243
|
+
showTimestamp: true,
|
|
244
|
+
persistToIndexedDB: true,
|
|
245
|
+
idbRetentionDays: 30,
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
// main.ts
|
|
250
|
+
import { environment } from './environments/environment';
|
|
251
|
+
import { provideLogger } from '@sixbell-telco/sdk/utils/logger';
|
|
252
|
+
|
|
253
|
+
bootstrapApplication(AppComponent, {
|
|
254
|
+
providers: [provideLogger(environment.logger)],
|
|
255
|
+
});
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Example 3: Error Reporting Integration
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
import { provideLogger } from '@sixbell-telco/sdk/utils/logger';
|
|
262
|
+
|
|
263
|
+
// With external error reporter (e.g., Sentry)
|
|
264
|
+
bootstrapApplication(AppComponent, {
|
|
265
|
+
providers: [
|
|
266
|
+
provideLogger({
|
|
267
|
+
enableLogging: true,
|
|
268
|
+
errorReporter: {
|
|
269
|
+
captureException: (error, context) => {
|
|
270
|
+
// Send to Sentry, LogRocket, etc.
|
|
271
|
+
console.log('Reporting error:', error, context);
|
|
272
|
+
},
|
|
273
|
+
},
|
|
274
|
+
onError: (error, context) => {
|
|
275
|
+
console.log('Error callback:', error, context);
|
|
276
|
+
},
|
|
277
|
+
}),
|
|
278
|
+
],
|
|
279
|
+
});
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
### Example 4: Silent Mode for Testing
|
|
283
|
+
|
|
284
|
+
```typescript
|
|
285
|
+
describe('MyService', () => {
|
|
286
|
+
let service: MyService;
|
|
287
|
+
let logger: LoggerService;
|
|
288
|
+
|
|
289
|
+
beforeEach(() => {
|
|
290
|
+
TestBed.configureTestingModule({
|
|
291
|
+
providers: [
|
|
292
|
+
MyService,
|
|
293
|
+
provideLogger({
|
|
294
|
+
enableLogging: true,
|
|
295
|
+
silentMode: true, // No console output during tests
|
|
296
|
+
logLevel: 'trace',
|
|
297
|
+
}),
|
|
298
|
+
],
|
|
299
|
+
});
|
|
300
|
+
logger = TestBed.inject(LoggerService);
|
|
301
|
+
service = TestBed.inject(MyService);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it('should log without console output', () => {
|
|
305
|
+
service.doSomething();
|
|
306
|
+
const logs = logger.getStoredLogs();
|
|
307
|
+
expect(logs.length).toBeGreaterThan(0);
|
|
308
|
+
});
|
|
309
|
+
});
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
### Example 5: Temporary Debug Logger
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
// In your service
|
|
316
|
+
private logger = inject(LoggerService);
|
|
317
|
+
|
|
318
|
+
debugComplexOperation() {
|
|
319
|
+
// Use debug-level logger just for this operation
|
|
320
|
+
const debugLogger = this.logger.withConfig({
|
|
321
|
+
logLevel: 'trace'
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
debugLogger.trace('Step 1: Starting complex operation');
|
|
325
|
+
debugLogger.trace('Step 2: Processing data');
|
|
326
|
+
debugLogger.trace('Step 3: Finalizing');
|
|
327
|
+
|
|
328
|
+
// Regular logger respects original config
|
|
329
|
+
this.logger.info('Complex operation completed');
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
### Example 6: Download Logs for Debugging
|
|
334
|
+
|
|
335
|
+
```typescript
|
|
336
|
+
// In a settings or debug component
|
|
337
|
+
downloadApplicationLogs() {
|
|
338
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
339
|
+
const filename = `app-logs-${timestamp}.json`;
|
|
340
|
+
|
|
341
|
+
this.logger.downloadLogs(filename);
|
|
342
|
+
}
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
## Logging Best Practices
|
|
346
|
+
|
|
347
|
+
### ✅ Do
|
|
348
|
+
|
|
349
|
+
- **Use appropriate log levels**: Use `info` for normal flow, `warn` for recoverable issues, `error` for failures
|
|
350
|
+
- **Add context**: Include component name and action to help debugging
|
|
351
|
+
- **Log at boundaries**: Log when entering/exiting major functions or components
|
|
352
|
+
- **Use descriptive messages**: Make it clear what happened
|
|
353
|
+
- **Include errors properly**: Pass Error objects to error/fatal methods
|
|
354
|
+
|
|
355
|
+
```typescript
|
|
356
|
+
// Good
|
|
357
|
+
this.logger.info('User authenticated', {
|
|
358
|
+
component: 'AuthService',
|
|
359
|
+
action: 'login',
|
|
360
|
+
userId: user.id,
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
try {
|
|
364
|
+
await this.api.fetch();
|
|
365
|
+
} catch (error) {
|
|
366
|
+
this.logger.error('API request failed', error as Error, {
|
|
367
|
+
component: 'DataService',
|
|
368
|
+
action: 'fetch',
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### ❌ Don't
|
|
374
|
+
|
|
375
|
+
- **Avoid logging sensitive data**: Don't log passwords, tokens, or personal information
|
|
376
|
+
- **Don't use error level for everything**: Use appropriate levels
|
|
377
|
+
- **Don't log in tight loops**: Can impact performance
|
|
378
|
+
- **Don't leave debug logs in production**: Use log level filtering instead
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
// Bad
|
|
382
|
+
this.logger.error('Password received: ' + password); // Security issue!
|
|
383
|
+
this.logger.error('Button clicked'); // Should be 'info'
|
|
384
|
+
|
|
385
|
+
for (let i = 0; i < 1000000; i++) {
|
|
386
|
+
this.logger.trace('Processing item ' + i); // Performance issue
|
|
387
|
+
}
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
## Color Scheme
|
|
391
|
+
|
|
392
|
+
The logger automatically applies theme-aware colors to console output.
|
|
393
|
+
|
|
394
|
+
### Light Mode (Default)
|
|
395
|
+
|
|
396
|
+
- Trace: Dark Gray
|
|
397
|
+
- Debug: Dark Blue
|
|
398
|
+
- Info: Dark Green
|
|
399
|
+
- Warn: Dark Orange
|
|
400
|
+
- Error: Dark Red
|
|
401
|
+
- Fatal: White on Red
|
|
402
|
+
|
|
403
|
+
### Dark Mode
|
|
404
|
+
|
|
405
|
+
- Trace: Light Gray
|
|
406
|
+
- Debug: Light Blue
|
|
407
|
+
- Info: Light Green
|
|
408
|
+
- Warn: Light Orange
|
|
409
|
+
- Error: Light Red
|
|
410
|
+
- Fatal: White on Light Red
|
|
411
|
+
|
|
412
|
+
## Storage
|
|
413
|
+
|
|
414
|
+
### In-Memory Storage
|
|
415
|
+
|
|
416
|
+
Logs are stored in memory by default (up to 1000 logs). Use `getStoredLogs()` to retrieve them.
|
|
417
|
+
|
|
418
|
+
### IndexedDB Persistence
|
|
419
|
+
|
|
420
|
+
Enable persistent storage for logs that survive page refreshes:
|
|
421
|
+
|
|
422
|
+
```typescript
|
|
423
|
+
provideLogger({
|
|
424
|
+
enableLogging: true,
|
|
425
|
+
persistToIndexedDB: true,
|
|
426
|
+
idbRetentionDays: 7, // Keep for 7 days
|
|
427
|
+
idbMaxLogs: 5000, // Store max 5000 logs
|
|
428
|
+
});
|
|
429
|
+
```
|
|
430
|
+
|
|
431
|
+
**Note**: IndexedDB is asynchronous. Use `await logger.getPersistedLogs()` to retrieve stored logs.
|
|
432
|
+
|
|
433
|
+
## Testing
|
|
434
|
+
|
|
435
|
+
The logger includes 50+ tests covering all functionality. Tests use Jest and ng-mocks patterns:
|
|
436
|
+
|
|
437
|
+
```typescript
|
|
438
|
+
import { TestBed } from '@angular/core/testing';
|
|
439
|
+
import { LoggerService } from '@sixbell-telco/sdk/utils/logger';
|
|
440
|
+
import { provideLogger } from '@sixbell-telco/sdk/utils/logger';
|
|
441
|
+
|
|
442
|
+
describe('LoggerService', () => {
|
|
443
|
+
let service: LoggerService;
|
|
444
|
+
|
|
445
|
+
beforeEach(() => {
|
|
446
|
+
TestBed.configureTestingModule({
|
|
447
|
+
providers: [provideLogger({ enableLogging: true, logLevel: 'trace' })],
|
|
448
|
+
});
|
|
449
|
+
service = TestBed.inject(LoggerService);
|
|
450
|
+
});
|
|
451
|
+
|
|
452
|
+
it('should log messages', () => {
|
|
453
|
+
service.info('Test message');
|
|
454
|
+
const logs = service.getStoredLogs();
|
|
455
|
+
expect(logs[0].message).toBe('Test message');
|
|
456
|
+
});
|
|
457
|
+
});
|
|
458
|
+
```
|
|
459
|
+
|
|
460
|
+
## Common Issues
|
|
461
|
+
|
|
462
|
+
### Logs Not Appearing
|
|
463
|
+
|
|
464
|
+
**Problem**: No logs in console
|
|
465
|
+
|
|
466
|
+
- Check `enableLogging` is `true`
|
|
467
|
+
- Check `logLevel` is set to capture your message level
|
|
468
|
+
- Check `silentMode` is `false`
|
|
469
|
+
|
|
470
|
+
### IndexedDB Not Persisting
|
|
471
|
+
|
|
472
|
+
**Problem**: Logs not saved to IndexedDB
|
|
473
|
+
|
|
474
|
+
- Check `persistToIndexedDB` is `true`
|
|
475
|
+
- Check browser supports IndexedDB (not disabled in settings)
|
|
476
|
+
- Verify logs using browser DevTools → Application → IndexedDB → sixbell_ui_kit
|
|
477
|
+
|
|
478
|
+
### Performance Issues
|
|
479
|
+
|
|
480
|
+
**Problem**: Console output is slow
|
|
481
|
+
|
|
482
|
+
- Reduce `logLevel` to only log important messages
|
|
483
|
+
- Use `silentMode: true` for high-frequency operations
|
|
484
|
+
- Avoid logging large objects
|
|
485
|
+
|
|
486
|
+
## API Types
|
|
487
|
+
|
|
488
|
+
```typescript
|
|
489
|
+
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
490
|
+
|
|
491
|
+
export interface LoggerContext {
|
|
492
|
+
component?: string;
|
|
493
|
+
action?: string;
|
|
494
|
+
timestamp?: number;
|
|
495
|
+
[key: string]: unknown; // Custom properties
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
export interface LoggerConfig {
|
|
499
|
+
enableLogging?: boolean;
|
|
500
|
+
logLevel?: LogLevel;
|
|
501
|
+
showTimestamp?: boolean;
|
|
502
|
+
showDate?: boolean;
|
|
503
|
+
silentMode?: boolean;
|
|
504
|
+
persistToIndexedDB?: boolean;
|
|
505
|
+
idbRetentionDays?: number;
|
|
506
|
+
idbMaxLogs?: number;
|
|
507
|
+
isDarkMode?: boolean;
|
|
508
|
+
onError?: (error: Error, context: LoggerContext) => void;
|
|
509
|
+
errorReporter?: {
|
|
510
|
+
captureException: (error: Error, context?: LoggerContext) => void;
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
IndexedDB requires modern browser support (IE not supported).
|
|
516
|
+
|
|
517
|
+
## Performance Considerations
|
|
518
|
+
|
|
519
|
+
- **In-Memory Storage**: Max 1000 logs to prevent memory bloat
|
|
520
|
+
- **IndexedDB**: Asynchronous, doesn't block main thread
|
|
521
|
+
- **Console Output**: Can be disabled with `silentMode`
|
|
522
|
+
- **Theme Detection**: Only initialized when logging is enabled
|
|
523
|
+
|
|
524
|
+
## Contributing
|
|
525
|
+
|
|
526
|
+
When adding features to the logger:
|
|
527
|
+
|
|
528
|
+
1. Update types in `models/logger.ts`
|
|
529
|
+
2. Add constants in `constants.ts`
|
|
530
|
+
3. Update `LoggerService` implementation
|
|
531
|
+
4. Add tests in `logger.service.spec.ts`
|
|
532
|
+
5. Update provider tests in `logger.provider.spec.ts`
|
|
533
|
+
6. Update this README with examples
|
|
534
|
+
|
|
535
|
+
## Related Documentation
|
|
536
|
+
|
|
537
|
+
- [UI Kit Overview](../../README.md)
|
|
538
|
+
- [Theme Utility](../theme/README.md)
|
|
539
|
+
- [Translation Utility](../translation/README.md)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger Constants
|
|
3
|
+
* Color schemes, log levels, and configuration defaults
|
|
4
|
+
*/
|
|
5
|
+
import type { LogLevel } from './models/logger';
|
|
6
|
+
export declare const LOG_LEVELS: Record<LogLevel, number>;
|
|
7
|
+
export declare const LOG_COLORS_LIGHT: Record<LogLevel, string>;
|
|
8
|
+
export declare const LOG_COLORS_DARK: Record<LogLevel, string>;
|
|
9
|
+
export declare const CONTEXT_COLOR_LIGHT: "color: #5500aa; font-weight: bold;";
|
|
10
|
+
export declare const CONTEXT_COLOR_DARK: "color: #dd99ff; font-weight: bold;";
|
|
11
|
+
export declare const TIMESTAMP_COLOR_LIGHT: "color: #006666; font-weight: bold;";
|
|
12
|
+
export declare const TIMESTAMP_COLOR_DARK: "color: #66ffff; font-weight: bold;";
|
|
13
|
+
export declare const IDB_STORE_NAME: "sixbell-logs";
|
|
14
|
+
export declare const IDB_DB_NAME: "sixbell-ui-kit";
|
|
15
|
+
export declare const MAX_STORED_LOGS: 1000;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import type { LoggerConfig, LoggerContext, LogLevel } from './models/logger';
|
|
2
|
+
import * as i0 from "@angular/core";
|
|
3
|
+
export declare class LoggerService {
|
|
4
|
+
private config;
|
|
5
|
+
private logStore;
|
|
6
|
+
private darkModeMediaQuery;
|
|
7
|
+
private darkModeListenerInitialized;
|
|
8
|
+
private idbReady;
|
|
9
|
+
/**
|
|
10
|
+
* Initialize automatic dark mode detection based on system preferences
|
|
11
|
+
* Only called when logging is enabled to minimize performance impact
|
|
12
|
+
*/
|
|
13
|
+
private initializeDarkModeDetection;
|
|
14
|
+
configure(config: Partial<LoggerConfig>): void;
|
|
15
|
+
/**
|
|
16
|
+
* Get all stored logs (useful for sending to external services)
|
|
17
|
+
*/
|
|
18
|
+
getStoredLogs(): {
|
|
19
|
+
timestamp: number;
|
|
20
|
+
level: LogLevel;
|
|
21
|
+
message: string;
|
|
22
|
+
context?: LoggerContext;
|
|
23
|
+
error?: Error;
|
|
24
|
+
}[];
|
|
25
|
+
/**
|
|
26
|
+
* Clear stored logs
|
|
27
|
+
*/
|
|
28
|
+
clearStoredLogs(): void;
|
|
29
|
+
/**
|
|
30
|
+
* Initialize IndexedDB for persistent log storage
|
|
31
|
+
* Only called when persistToIndexedDB is enabled
|
|
32
|
+
*/
|
|
33
|
+
private initializeIndexedDB;
|
|
34
|
+
/**
|
|
35
|
+
* Persist a log entry to IndexedDB
|
|
36
|
+
*/
|
|
37
|
+
private persistLogToIndexedDB;
|
|
38
|
+
/**
|
|
39
|
+
* Clean up old logs based on retention policy (days and max count)
|
|
40
|
+
*/
|
|
41
|
+
private cleanupOldLogs;
|
|
42
|
+
/**
|
|
43
|
+
* Retrieve logs from IndexedDB
|
|
44
|
+
*/
|
|
45
|
+
getPersistedLogs(): Promise<Array<{
|
|
46
|
+
id?: number;
|
|
47
|
+
timestamp: number;
|
|
48
|
+
level: LogLevel;
|
|
49
|
+
message: string;
|
|
50
|
+
context?: LoggerContext;
|
|
51
|
+
error?: {
|
|
52
|
+
name: string;
|
|
53
|
+
message: string;
|
|
54
|
+
stack?: string;
|
|
55
|
+
};
|
|
56
|
+
}>>;
|
|
57
|
+
/**
|
|
58
|
+
* Clear persisted logs from IndexedDB
|
|
59
|
+
*/
|
|
60
|
+
clearPersistedLogs(): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Download logs as a JSON file
|
|
63
|
+
* Combines in-memory logs with persisted IndexedDB logs
|
|
64
|
+
*/
|
|
65
|
+
downloadLogs(filename?: string): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Update theme mode for color adjustment
|
|
68
|
+
* If you want to restore automatic detection, pass undefined
|
|
69
|
+
*
|
|
70
|
+
* @param isDark true for dark mode, false for light mode, undefined to use system preference
|
|
71
|
+
*/
|
|
72
|
+
setDarkMode(isDark: boolean | undefined): void;
|
|
73
|
+
/**
|
|
74
|
+
* Get current dark mode status
|
|
75
|
+
*/
|
|
76
|
+
isDarkMode(): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Create a temporary logger with override configuration.
|
|
79
|
+
* Useful for local use-case-specific logging without global provider changes.
|
|
80
|
+
*
|
|
81
|
+
* @param overrideConfig Configuration that overrides current logger settings
|
|
82
|
+
* @returns A new LoggerService instance with merged config
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```typescript
|
|
86
|
+
* // In theme service:
|
|
87
|
+
* private readonly logger = inject(LoggerService);
|
|
88
|
+
*
|
|
89
|
+
* // For this specific operation, use debug level:
|
|
90
|
+
* const debugLogger = this.logger.withConfig({ logLevel: 'debug' });
|
|
91
|
+
* debugLogger.debug('Detailed operation trace');
|
|
92
|
+
*
|
|
93
|
+
* // Back to global config:
|
|
94
|
+
* this.logger.debug('This respects global config');
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
withConfig(overrideConfig: Partial<LoggerConfig>): LoggerService;
|
|
98
|
+
private shouldLog;
|
|
99
|
+
private getColors;
|
|
100
|
+
private getContextColor;
|
|
101
|
+
private getTimestampColor;
|
|
102
|
+
private storeLog;
|
|
103
|
+
/**
|
|
104
|
+
* Extract component name from message pattern like "[ComponentName] message"
|
|
105
|
+
* and return formatted parts for colored logging
|
|
106
|
+
*/
|
|
107
|
+
private formatMessageWithContext;
|
|
108
|
+
/**
|
|
109
|
+
* Format complete log output with level, timestamp, context, and message
|
|
110
|
+
* Returns array of format string and all styles for console output
|
|
111
|
+
*/
|
|
112
|
+
private formatCompleteLogOutput;
|
|
113
|
+
trace(message: string, context?: LoggerContext): void;
|
|
114
|
+
debug(message: string, context?: LoggerContext): void;
|
|
115
|
+
info(message: string, context?: LoggerContext): void;
|
|
116
|
+
warn(message: string, context?: LoggerContext): void;
|
|
117
|
+
error(message: string, error?: Error, context?: LoggerContext): void;
|
|
118
|
+
fatal(message: string, error?: Error, context?: LoggerContext): void;
|
|
119
|
+
private reportToExternal;
|
|
120
|
+
private createContext;
|
|
121
|
+
static ɵfac: i0.ɵɵFactoryDeclaration<LoggerService, never>;
|
|
122
|
+
static ɵprov: i0.ɵɵInjectableDeclaration<LoggerService>;
|
|
123
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger Types and Interfaces
|
|
3
|
+
*/
|
|
4
|
+
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
|
|
5
|
+
export interface LoggerContext {
|
|
6
|
+
component?: string;
|
|
7
|
+
action?: string;
|
|
8
|
+
timestamp?: number;
|
|
9
|
+
[key: string]: unknown;
|
|
10
|
+
}
|
|
11
|
+
export interface LoggerConfig {
|
|
12
|
+
enableLogging?: boolean;
|
|
13
|
+
logLevel?: LogLevel;
|
|
14
|
+
showTimestamp?: boolean;
|
|
15
|
+
showDate?: boolean;
|
|
16
|
+
silentMode?: boolean;
|
|
17
|
+
persistToIndexedDB?: boolean;
|
|
18
|
+
idbRetentionDays?: number;
|
|
19
|
+
idbMaxLogs?: number;
|
|
20
|
+
onError?: (error: Error, context: LoggerContext) => void;
|
|
21
|
+
errorReporter?: {
|
|
22
|
+
captureException: (error: Error, context?: LoggerContext) => void;
|
|
23
|
+
};
|
|
24
|
+
isDarkMode?: boolean;
|
|
25
|
+
}
|