content-security-toolkit 1.0.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/LICENSE +9 -0
- package/README.md +171 -0
- package/dist/config/default-extensions-config.json +103 -0
- package/dist/core/ContentProtector.d.ts +63 -0
- package/dist/core/ContentProtector.js +279 -0
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +2 -0
- package/dist/core/mediator/ContentProtectionMediator.d.ts +86 -0
- package/dist/core/mediator/ContentProtectionMediator.js +238 -0
- package/dist/core/mediator/eventDataTypes.d.ts +205 -0
- package/dist/core/mediator/eventDataTypes.js +23 -0
- package/dist/core/mediator/handlers/abstractEventHandler.d.ts +67 -0
- package/dist/core/mediator/handlers/abstractEventHandler.js +106 -0
- package/dist/core/mediator/handlers/baseEventHandler.d.ts +65 -0
- package/dist/core/mediator/handlers/baseEventHandler.js +99 -0
- package/dist/core/mediator/handlers/devToolsEventHandler.d.ts +9 -0
- package/dist/core/mediator/handlers/devToolsEventHandler.js +95 -0
- package/dist/core/mediator/handlers/eventHandlerRegistry.d.ts +9 -0
- package/dist/core/mediator/handlers/eventHandlerRegistry.js +34 -0
- package/dist/core/mediator/handlers/extensionEventHandlers.d.ts +40 -0
- package/dist/core/mediator/handlers/extensionEventHandlers.js +140 -0
- package/dist/core/mediator/handlers/iFrameEventHandlers.d.ts +27 -0
- package/dist/core/mediator/handlers/iFrameEventHandlers.js +93 -0
- package/dist/core/mediator/handlers/index.d.ts +9 -0
- package/dist/core/mediator/handlers/index.js +34 -0
- package/dist/core/mediator/handlers/screenShotEventHandlers.d.ts +34 -0
- package/dist/core/mediator/handlers/screenShotEventHandlers.js +111 -0
- package/dist/core/mediator/protection-event.d.ts +94 -0
- package/dist/core/mediator/protection-event.js +43 -0
- package/dist/core/mediator/types.d.ts +105 -0
- package/dist/core/mediator/types.js +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/strategies/AbstractStrategy.d.ts +152 -0
- package/dist/strategies/AbstractStrategy.js +296 -0
- package/dist/strategies/AbstractStrategy.mediator.d.ts +162 -0
- package/dist/strategies/AbstractStrategy.mediator.js +349 -0
- package/dist/strategies/ClipboardStrategy.d.ts +67 -0
- package/dist/strategies/ClipboardStrategy.js +291 -0
- package/dist/strategies/ContextMenuStrategy.d.ts +60 -0
- package/dist/strategies/ContextMenuStrategy.js +454 -0
- package/dist/strategies/DevToolsStrategy copy.d.ts +85 -0
- package/dist/strategies/DevToolsStrategy copy.js +362 -0
- package/dist/strategies/DevToolsStrategy-detectorManager.d.ts +70 -0
- package/dist/strategies/DevToolsStrategy-detectorManager.js +309 -0
- package/dist/strategies/DevToolsStrategy-simple.d.ts +75 -0
- package/dist/strategies/DevToolsStrategy-simple.js +366 -0
- package/dist/strategies/DevToolsStrategy.d.ts +55 -0
- package/dist/strategies/DevToolsStrategy.js +314 -0
- package/dist/strategies/ExtensionStrategy.d.ts +66 -0
- package/dist/strategies/ExtensionStrategy.js +486 -0
- package/dist/strategies/IFrameStrategy.d.ts +49 -0
- package/dist/strategies/IFrameStrategy.js +255 -0
- package/dist/strategies/KeyboardStrategy.d.ts +35 -0
- package/dist/strategies/KeyboardStrategy.js +130 -0
- package/dist/strategies/PrintStrategy.d.ts +47 -0
- package/dist/strategies/PrintStrategy.js +201 -0
- package/dist/strategies/ScreenshotStrategy.d.ts +90 -0
- package/dist/strategies/ScreenshotStrategy.js +488 -0
- package/dist/strategies/SelectionStrategy.d.ts +49 -0
- package/dist/strategies/SelectionStrategy.js +216 -0
- package/dist/strategies/StrategyRegistry.d.ts +133 -0
- package/dist/strategies/StrategyRegistry.js +379 -0
- package/dist/strategies/WatermarkStrategy.d.ts +47 -0
- package/dist/strategies/WatermarkStrategy.js +273 -0
- package/dist/strategies/index.d.ts +9 -0
- package/dist/strategies/index.js +10 -0
- package/dist/types/index.d.ts +271 -0
- package/dist/types/index.js +16 -0
- package/dist/utils/DOMObserver.d.ts +68 -0
- package/dist/utils/DOMObserver.js +134 -0
- package/dist/utils/base/LoggableComponent.d.ts +62 -0
- package/dist/utils/base/LoggableComponent.js +95 -0
- package/dist/utils/debuggerDetector/debuggerDetectionWorker.d.ts +6 -0
- package/dist/utils/debuggerDetector/debuggerDetectionWorker.js +24 -0
- package/dist/utils/debuggerDetector/debuggerDetector.d.ts +55 -0
- package/dist/utils/debuggerDetector/debuggerDetector.js +158 -0
- package/dist/utils/debuggerDetector/firefoxDetector.d.ts +8 -0
- package/dist/utils/debuggerDetector/firefoxDetector.js +64 -0
- package/dist/utils/detection.d.ts +29 -0
- package/dist/utils/detection.js +267 -0
- package/dist/utils/detectors/AbstractDevToolsDetector.d.ts +105 -0
- package/dist/utils/detectors/AbstractDevToolsDetector.js +136 -0
- package/dist/utils/detectors/dateToStringDetector.d.ts +43 -0
- package/dist/utils/detectors/dateToStringDetector.js +96 -0
- package/dist/utils/detectors/debugLibDetector.d.ts +64 -0
- package/dist/utils/detectors/debugLibDetector.js +195 -0
- package/dist/utils/detectors/debuggerDetectionWorker.d.ts +6 -0
- package/dist/utils/detectors/debuggerDetectionWorker.js +24 -0
- package/dist/utils/detectors/debuggerDetector.d.ts +51 -0
- package/dist/utils/detectors/debuggerDetector.js +211 -0
- package/dist/utils/detectors/defineGetterDetector.d.ts +48 -0
- package/dist/utils/detectors/defineGetterDetector.js +150 -0
- package/dist/utils/detectors/detectorInterface.d.ts +36 -0
- package/dist/utils/detectors/detectorInterface.js +1 -0
- package/dist/utils/detectors/devToolsDetectorManager.d.ts +88 -0
- package/dist/utils/detectors/devToolsDetectorManager.js +246 -0
- package/dist/utils/detectors/firefoxDetector.d.ts +8 -0
- package/dist/utils/detectors/firefoxDetector.js +64 -0
- package/dist/utils/detectors/funcToStringDetector.d.ts +43 -0
- package/dist/utils/detectors/funcToStringDetector.js +90 -0
- package/dist/utils/detectors/regToStringDetector.d.ts +43 -0
- package/dist/utils/detectors/regToStringDetector.js +129 -0
- package/dist/utils/detectors/sizeDetector.d.ts +54 -0
- package/dist/utils/detectors/sizeDetector.js +134 -0
- package/dist/utils/detectors/timingDetector.d.ts +55 -0
- package/dist/utils/detectors/timingDetector.js +143 -0
- package/dist/utils/dom.d.ts +20 -0
- package/dist/utils/dom.js +83 -0
- package/dist/utils/environment.d.ts +29 -0
- package/dist/utils/environment.js +267 -0
- package/dist/utils/eventManager.d.ts +167 -0
- package/dist/utils/eventManager.js +556 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/intervalManager.d.ts +96 -0
- package/dist/utils/intervalManager.js +229 -0
- package/dist/utils/keyboardShortcutManager/keyboardShortcutManager.d.ts +41 -0
- package/dist/utils/keyboardShortcutManager/keyboardShortcutManager.js +135 -0
- package/dist/utils/keyboardShortcutManager/keyboardShortcuts.d.ts +18 -0
- package/dist/utils/keyboardShortcutManager/keyboardShortcuts.js +195 -0
- package/dist/utils/logging/LogLevel.d.ts +21 -0
- package/dist/utils/logging/LogLevel.js +46 -0
- package/dist/utils/logging/LoggingConfig.d.ts +68 -0
- package/dist/utils/logging/LoggingConfig.js +64 -0
- package/dist/utils/logging/LoggingFactory.d.ts +22 -0
- package/dist/utils/logging/LoggingFactory.js +61 -0
- package/dist/utils/logging/LoggingService.d.ts +235 -0
- package/dist/utils/logging/LoggingService.js +385 -0
- package/dist/utils/logging/SimpleLoggingService.d.ts +39 -0
- package/dist/utils/logging/SimpleLoggingService.js +58 -0
- package/dist/utils/logging/advanced/LogLevel.d.ts +21 -0
- package/dist/utils/logging/advanced/LogLevel.js +46 -0
- package/dist/utils/logging/advanced/LoggingConfig.d.ts +68 -0
- package/dist/utils/logging/advanced/LoggingConfig.js +64 -0
- package/dist/utils/logging/advanced/LoggingFactory.d.ts +22 -0
- package/dist/utils/logging/advanced/LoggingFactory.js +61 -0
- package/dist/utils/logging/advanced/LoggingService.d.ts +235 -0
- package/dist/utils/logging/advanced/LoggingService.js +385 -0
- package/dist/utils/logging/simple/Loggable.d.ts +33 -0
- package/dist/utils/logging/simple/Loggable.js +1 -0
- package/dist/utils/logging/simple/LoggingDelegate.d.ts +42 -0
- package/dist/utils/logging/simple/LoggingDelegate.js +53 -0
- package/dist/utils/logging/simple/SimpleLoggingService.d.ts +39 -0
- package/dist/utils/logging/simple/SimpleLoggingService.js +58 -0
- package/dist/utils/orientation.d.ts +15 -0
- package/dist/utils/orientation.js +32 -0
- package/dist/utils/protectedContentManager-simple.d.ts +86 -0
- package/dist/utils/protectedContentManager-simple.js +180 -0
- package/dist/utils/protectedContentManager.d.ts +162 -0
- package/dist/utils/protectedContentManager.js +427 -0
- package/dist/utils/screenshotDetector.d.ts +72 -0
- package/dist/utils/screenshotDetector.js +179 -0
- package/dist/utils/securityOverlayManager-observer-pause.d.ts +283 -0
- package/dist/utils/securityOverlayManager-observer-pause.js +878 -0
- package/dist/utils/securityOverlayManager-simple.d.ts +197 -0
- package/dist/utils/securityOverlayManager-simple.js +552 -0
- package/dist/utils/securityOverlayManager.d.ts +260 -0
- package/dist/utils/securityOverlayManager.js +774 -0
- package/dist/utils/timeoutManager.d.ts +55 -0
- package/dist/utils/timeoutManager.js +121 -0
- package/package.json +54 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { LoggingService } from "./LoggingService";
|
|
2
|
+
import { getComponentLogLevel, getLoggingConfig } from "./LoggingConfig";
|
|
3
|
+
/**
|
|
4
|
+
* Factory for creating LoggingService instances
|
|
5
|
+
*/
|
|
6
|
+
export class LoggingFactory {
|
|
7
|
+
/**
|
|
8
|
+
* Get a LoggingService instance for a component
|
|
9
|
+
* @param componentName Component name
|
|
10
|
+
* @returns LoggingService instance
|
|
11
|
+
*/
|
|
12
|
+
static getLogger(componentName) {
|
|
13
|
+
// Check if we already have an instance for this component
|
|
14
|
+
if (this.instances.has(componentName)) {
|
|
15
|
+
return this.instances.get(componentName);
|
|
16
|
+
}
|
|
17
|
+
// Get the global config
|
|
18
|
+
const config = getLoggingConfig();
|
|
19
|
+
// Create a new instance
|
|
20
|
+
const logger = new LoggingService({
|
|
21
|
+
componentName,
|
|
22
|
+
level: getComponentLogLevel(componentName),
|
|
23
|
+
useColors: config.useColors,
|
|
24
|
+
includeTimestamps: config.includeTimestamps,
|
|
25
|
+
includeComponentName: config.includeComponentName,
|
|
26
|
+
});
|
|
27
|
+
// Apply global settings
|
|
28
|
+
if (config.enabledComponents !== null) {
|
|
29
|
+
logger.setEnabledComponents(config.enabledComponents);
|
|
30
|
+
}
|
|
31
|
+
if (config.focusedLevel !== null) {
|
|
32
|
+
logger.focusLevel(config.focusedLevel);
|
|
33
|
+
}
|
|
34
|
+
// Store the instance
|
|
35
|
+
this.instances.set(componentName, logger);
|
|
36
|
+
return logger;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Update all existing logger instances with current configuration
|
|
40
|
+
*/
|
|
41
|
+
static updateAllLoggers() {
|
|
42
|
+
const config = getLoggingConfig();
|
|
43
|
+
for (const [componentName, logger] of this.instances.entries()) {
|
|
44
|
+
logger.setLevel(getComponentLogLevel(componentName));
|
|
45
|
+
if (config.enabledComponents !== null) {
|
|
46
|
+
logger.setEnabledComponents(config.enabledComponents);
|
|
47
|
+
}
|
|
48
|
+
if (config.focusedLevel !== null) {
|
|
49
|
+
logger.focusLevel(config.focusedLevel);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get all registered logger instances
|
|
55
|
+
* @returns Map of component names to logger instances
|
|
56
|
+
*/
|
|
57
|
+
static getAllLoggers() {
|
|
58
|
+
return new Map(this.instances);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
LoggingFactory.instances = new Map();
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { LogLevel } from "./LogLevel";
|
|
2
|
+
/**
|
|
3
|
+
* Options for the LoggingService
|
|
4
|
+
*/
|
|
5
|
+
export interface LoggingOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Component name for log prefixing
|
|
8
|
+
*/
|
|
9
|
+
componentName: string;
|
|
10
|
+
/**
|
|
11
|
+
* Initial log level
|
|
12
|
+
* @default LogLevel.INFO
|
|
13
|
+
*/
|
|
14
|
+
level?: LogLevel;
|
|
15
|
+
/**
|
|
16
|
+
* Whether logging is enabled
|
|
17
|
+
* @default true
|
|
18
|
+
*/
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Whether to use colors in console output
|
|
22
|
+
* @default true
|
|
23
|
+
*/
|
|
24
|
+
useColors?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Whether to include timestamps in logs
|
|
27
|
+
* @default true
|
|
28
|
+
*/
|
|
29
|
+
includeTimestamps?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Whether to include component name in logs
|
|
32
|
+
* @default true
|
|
33
|
+
*/
|
|
34
|
+
includeComponentName?: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Performance timing result
|
|
38
|
+
*/
|
|
39
|
+
export interface TimingResult<T> {
|
|
40
|
+
result: T;
|
|
41
|
+
duration: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Advanced logging service with context tracking, filtering, and performance measurement
|
|
45
|
+
*/
|
|
46
|
+
export declare class LoggingService {
|
|
47
|
+
private componentName;
|
|
48
|
+
private currentLevel;
|
|
49
|
+
private enabled;
|
|
50
|
+
private useColors;
|
|
51
|
+
private includeTimestamps;
|
|
52
|
+
private includeComponentName;
|
|
53
|
+
private contextStack;
|
|
54
|
+
private contextDepth;
|
|
55
|
+
private enabledComponents;
|
|
56
|
+
private focusedLevel;
|
|
57
|
+
private groupDepth;
|
|
58
|
+
/**
|
|
59
|
+
* Create a new LoggingService
|
|
60
|
+
* @param options Configuration options
|
|
61
|
+
*/
|
|
62
|
+
constructor(options: LoggingOptions);
|
|
63
|
+
/**
|
|
64
|
+
* Set the current log level threshold
|
|
65
|
+
* @param level New log level
|
|
66
|
+
*/
|
|
67
|
+
setLevel(level: LogLevel): void;
|
|
68
|
+
/**
|
|
69
|
+
* Get the current log level
|
|
70
|
+
* @returns The current log level
|
|
71
|
+
*/
|
|
72
|
+
getLevel(): LogLevel;
|
|
73
|
+
/**
|
|
74
|
+
* Get the current log level name
|
|
75
|
+
* @returns The current log level name
|
|
76
|
+
*/
|
|
77
|
+
getLevelName(): string;
|
|
78
|
+
/**
|
|
79
|
+
* Enable or disable logging
|
|
80
|
+
* @param enabled Whether logging should be enabled
|
|
81
|
+
*/
|
|
82
|
+
setEnabled(enabled: boolean): void;
|
|
83
|
+
/**
|
|
84
|
+
* Check if logging is enabled
|
|
85
|
+
* @returns Whether logging is enabled
|
|
86
|
+
*/
|
|
87
|
+
isEnabled(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Set components to show logs for
|
|
90
|
+
* @param components Array of component names to enable, or null to enable all
|
|
91
|
+
*/
|
|
92
|
+
setEnabledComponents(components: string[] | null): void;
|
|
93
|
+
/**
|
|
94
|
+
* Focus on a specific log level
|
|
95
|
+
* @param level Level to focus on, or null to show all levels
|
|
96
|
+
*/
|
|
97
|
+
focusLevel(level: LogLevel | null): void;
|
|
98
|
+
/**
|
|
99
|
+
* Increase context depth (call when entering a loop or nested function)
|
|
100
|
+
* @param contextName Optional name for the context
|
|
101
|
+
* @returns A function that exits this context when called
|
|
102
|
+
*/
|
|
103
|
+
enterContext(contextName?: string): () => void;
|
|
104
|
+
/**
|
|
105
|
+
* Decrease context depth (call when exiting a loop or nested function)
|
|
106
|
+
*/
|
|
107
|
+
exitContext(): void;
|
|
108
|
+
/**
|
|
109
|
+
* Execute a function within a named context
|
|
110
|
+
* @param contextName Name for the context
|
|
111
|
+
* @param fn Function to execute
|
|
112
|
+
* @returns The result of the function
|
|
113
|
+
*/
|
|
114
|
+
withContext<T>(contextName: string, fn: () => T): T;
|
|
115
|
+
/**
|
|
116
|
+
* Execute an async function within a named context
|
|
117
|
+
* @param contextName Name for the context
|
|
118
|
+
* @param fn Async function to execute
|
|
119
|
+
* @returns Promise resolving to the result of the function
|
|
120
|
+
*/
|
|
121
|
+
withContextAsync<T>(contextName: string, fn: () => Promise<T>): Promise<T>;
|
|
122
|
+
/**
|
|
123
|
+
* Start a console group
|
|
124
|
+
* @param label Label for the group
|
|
125
|
+
* @param collapsed Whether the group should be collapsed
|
|
126
|
+
*/
|
|
127
|
+
startGroup(label: string, collapsed?: boolean): void;
|
|
128
|
+
/**
|
|
129
|
+
* End the current console group
|
|
130
|
+
*/
|
|
131
|
+
endGroup(): void;
|
|
132
|
+
/**
|
|
133
|
+
* Execute a function within a console group
|
|
134
|
+
* @param label Label for the group
|
|
135
|
+
* @param fn Function to execute
|
|
136
|
+
* @param collapsed Whether the group should be collapsed
|
|
137
|
+
* @returns The result of the function
|
|
138
|
+
*/
|
|
139
|
+
group<T>(label: string, fn: () => T, collapsed?: boolean): T;
|
|
140
|
+
/**
|
|
141
|
+
* Execute an async function within a console group
|
|
142
|
+
* @param label Label for the group
|
|
143
|
+
* @param fn Async function to execute
|
|
144
|
+
* @param collapsed Whether the group should be collapsed
|
|
145
|
+
* @returns Promise resolving to the result of the function
|
|
146
|
+
*/
|
|
147
|
+
groupAsync<T>(label: string, fn: () => Promise<T>, collapsed?: boolean): Promise<T>;
|
|
148
|
+
/**
|
|
149
|
+
* Measure the execution time of a function
|
|
150
|
+
* @param label Label for the timing
|
|
151
|
+
* @param fn Function to measure
|
|
152
|
+
* @returns Object containing the result and duration
|
|
153
|
+
*/
|
|
154
|
+
time<T>(label: string, fn: () => T): TimingResult<T>;
|
|
155
|
+
/**
|
|
156
|
+
* Measure the execution time of an async function
|
|
157
|
+
* @param label Label for the timing
|
|
158
|
+
* @param fn Async function to measure
|
|
159
|
+
* @returns Promise resolving to an object containing the result and duration
|
|
160
|
+
*/
|
|
161
|
+
timeAsync<T>(label: string, fn: () => Promise<T>): Promise<TimingResult<T>>;
|
|
162
|
+
/**
|
|
163
|
+
* Log with automatic level adjustment based on context depth
|
|
164
|
+
* @param message Message to log
|
|
165
|
+
* @param args Additional arguments to log
|
|
166
|
+
*/
|
|
167
|
+
logWithContext(message: string, ...args: unknown[]): void;
|
|
168
|
+
/**
|
|
169
|
+
* Main logging method
|
|
170
|
+
* @param level Log level
|
|
171
|
+
* @param message Message to log
|
|
172
|
+
* @param args Additional arguments to log
|
|
173
|
+
*/
|
|
174
|
+
log(level: LogLevel, message: string, ...args: unknown[]): void;
|
|
175
|
+
/**
|
|
176
|
+
* Log an error message
|
|
177
|
+
* @param message Error message
|
|
178
|
+
* @param args Additional arguments to log
|
|
179
|
+
*/
|
|
180
|
+
error(message: string, ...args: unknown[]): void;
|
|
181
|
+
/**
|
|
182
|
+
* Log a warning message
|
|
183
|
+
* @param message Warning message
|
|
184
|
+
* @param args Additional arguments to log
|
|
185
|
+
*/
|
|
186
|
+
warn(message: string, ...args: unknown[]): void;
|
|
187
|
+
/**
|
|
188
|
+
* Log an info message
|
|
189
|
+
* @param message Info message
|
|
190
|
+
* @param args Additional arguments to log
|
|
191
|
+
*/
|
|
192
|
+
info(message: string, ...args: unknown[]): void;
|
|
193
|
+
/**
|
|
194
|
+
* Log a debug message
|
|
195
|
+
* @param message Debug message
|
|
196
|
+
* @param args Additional arguments to log
|
|
197
|
+
*/
|
|
198
|
+
debug(message: string, ...args: unknown[]): void;
|
|
199
|
+
/**
|
|
200
|
+
* Log a trace message
|
|
201
|
+
* @param message Trace message
|
|
202
|
+
* @param args Additional arguments to log
|
|
203
|
+
*/
|
|
204
|
+
trace(message: string, ...args: unknown[]): void;
|
|
205
|
+
/**
|
|
206
|
+
* Log a verbose message
|
|
207
|
+
* @param message Verbose message
|
|
208
|
+
* @param args Additional arguments to log
|
|
209
|
+
*/
|
|
210
|
+
verbose(message: string, ...args: unknown[]): void;
|
|
211
|
+
/**
|
|
212
|
+
* Check if a message should be logged
|
|
213
|
+
* @param level Log level of the message
|
|
214
|
+
* @returns Whether the message should be logged
|
|
215
|
+
*/
|
|
216
|
+
private shouldLog;
|
|
217
|
+
/**
|
|
218
|
+
* Format a log message
|
|
219
|
+
* @param level Log level
|
|
220
|
+
* @param message Message to format
|
|
221
|
+
* @returns Formatted message
|
|
222
|
+
*/
|
|
223
|
+
private formatMessage;
|
|
224
|
+
/**
|
|
225
|
+
* Get the current timestamp as a string
|
|
226
|
+
* @returns Formatted timestamp
|
|
227
|
+
*/
|
|
228
|
+
private getTimestamp;
|
|
229
|
+
/**
|
|
230
|
+
* Get prefix for log level
|
|
231
|
+
* @param level Log level
|
|
232
|
+
* @returns Formatted prefix
|
|
233
|
+
*/
|
|
234
|
+
private getLevelPrefix;
|
|
235
|
+
}
|
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import { LogLevel, getLogLevelName } from "./LogLevel";
|
|
2
|
+
/**
|
|
3
|
+
* Advanced logging service with context tracking, filtering, and performance measurement
|
|
4
|
+
*/
|
|
5
|
+
export class LoggingService {
|
|
6
|
+
/**
|
|
7
|
+
* Create a new LoggingService
|
|
8
|
+
* @param options Configuration options
|
|
9
|
+
*/
|
|
10
|
+
constructor(options) {
|
|
11
|
+
// Context tracking
|
|
12
|
+
this.contextStack = [];
|
|
13
|
+
this.contextDepth = 0;
|
|
14
|
+
// Component filtering
|
|
15
|
+
this.enabledComponents = null;
|
|
16
|
+
// Level focusing
|
|
17
|
+
this.focusedLevel = null;
|
|
18
|
+
// Console group tracking
|
|
19
|
+
this.groupDepth = 0;
|
|
20
|
+
this.componentName = options.componentName;
|
|
21
|
+
this.currentLevel = options.level ?? LogLevel.INFO;
|
|
22
|
+
this.enabled = options.enabled ?? true;
|
|
23
|
+
this.useColors = options.useColors ?? true;
|
|
24
|
+
this.includeTimestamps = options.includeTimestamps ?? true;
|
|
25
|
+
this.includeComponentName = options.includeComponentName ?? true;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Set the current log level threshold
|
|
29
|
+
* @param level New log level
|
|
30
|
+
*/
|
|
31
|
+
setLevel(level) {
|
|
32
|
+
this.currentLevel = level;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get the current log level
|
|
36
|
+
* @returns The current log level
|
|
37
|
+
*/
|
|
38
|
+
getLevel() {
|
|
39
|
+
return this.currentLevel;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get the current log level name
|
|
43
|
+
* @returns The current log level name
|
|
44
|
+
*/
|
|
45
|
+
getLevelName() {
|
|
46
|
+
return getLogLevelName(this.currentLevel);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Enable or disable logging
|
|
50
|
+
* @param enabled Whether logging should be enabled
|
|
51
|
+
*/
|
|
52
|
+
setEnabled(enabled) {
|
|
53
|
+
this.enabled = enabled;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check if logging is enabled
|
|
57
|
+
* @returns Whether logging is enabled
|
|
58
|
+
*/
|
|
59
|
+
isEnabled() {
|
|
60
|
+
return this.enabled;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Set components to show logs for
|
|
64
|
+
* @param components Array of component names to enable, or null to enable all
|
|
65
|
+
*/
|
|
66
|
+
setEnabledComponents(components) {
|
|
67
|
+
if (components === null) {
|
|
68
|
+
this.enabledComponents = null;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
this.enabledComponents = new Set(components);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Focus on a specific log level
|
|
76
|
+
* @param level Level to focus on, or null to show all levels
|
|
77
|
+
*/
|
|
78
|
+
focusLevel(level) {
|
|
79
|
+
this.focusedLevel = level;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Increase context depth (call when entering a loop or nested function)
|
|
83
|
+
* @param contextName Optional name for the context
|
|
84
|
+
* @returns A function that exits this context when called
|
|
85
|
+
*/
|
|
86
|
+
enterContext(contextName = "context") {
|
|
87
|
+
this.contextDepth++;
|
|
88
|
+
const context = {
|
|
89
|
+
name: contextName,
|
|
90
|
+
startTime: performance.now(),
|
|
91
|
+
depth: this.contextDepth,
|
|
92
|
+
};
|
|
93
|
+
this.contextStack.push(context);
|
|
94
|
+
this.trace(`Entering ${contextName}`);
|
|
95
|
+
// Return a function that exits this context
|
|
96
|
+
return () => {
|
|
97
|
+
this.exitContext();
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Decrease context depth (call when exiting a loop or nested function)
|
|
102
|
+
*/
|
|
103
|
+
exitContext() {
|
|
104
|
+
if (this.contextStack.length > 0) {
|
|
105
|
+
const context = this.contextStack.pop();
|
|
106
|
+
this.contextDepth = Math.max(0, this.contextDepth - 1);
|
|
107
|
+
const duration = performance.now() - context.startTime;
|
|
108
|
+
this.trace(`Exiting ${context.name} (took ${duration.toFixed(2)}ms)`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Execute a function within a named context
|
|
113
|
+
* @param contextName Name for the context
|
|
114
|
+
* @param fn Function to execute
|
|
115
|
+
* @returns The result of the function
|
|
116
|
+
*/
|
|
117
|
+
withContext(contextName, fn) {
|
|
118
|
+
const exitFn = this.enterContext(contextName);
|
|
119
|
+
try {
|
|
120
|
+
return fn();
|
|
121
|
+
}
|
|
122
|
+
finally {
|
|
123
|
+
exitFn();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Execute an async function within a named context
|
|
128
|
+
* @param contextName Name for the context
|
|
129
|
+
* @param fn Async function to execute
|
|
130
|
+
* @returns Promise resolving to the result of the function
|
|
131
|
+
*/
|
|
132
|
+
async withContextAsync(contextName, fn) {
|
|
133
|
+
const exitFn = this.enterContext(contextName);
|
|
134
|
+
try {
|
|
135
|
+
return await fn();
|
|
136
|
+
}
|
|
137
|
+
finally {
|
|
138
|
+
exitFn();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Start a console group
|
|
143
|
+
* @param label Label for the group
|
|
144
|
+
* @param collapsed Whether the group should be collapsed
|
|
145
|
+
*/
|
|
146
|
+
startGroup(label, collapsed = false) {
|
|
147
|
+
if (!this.shouldLog(LogLevel.INFO))
|
|
148
|
+
return;
|
|
149
|
+
this.groupDepth++;
|
|
150
|
+
if (collapsed) {
|
|
151
|
+
console.groupCollapsed(this.formatMessage(LogLevel.INFO, label));
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
console.group(this.formatMessage(LogLevel.INFO, label));
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* End the current console group
|
|
159
|
+
*/
|
|
160
|
+
endGroup() {
|
|
161
|
+
if (this.groupDepth > 0) {
|
|
162
|
+
console.groupEnd();
|
|
163
|
+
this.groupDepth--;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Execute a function within a console group
|
|
168
|
+
* @param label Label for the group
|
|
169
|
+
* @param fn Function to execute
|
|
170
|
+
* @param collapsed Whether the group should be collapsed
|
|
171
|
+
* @returns The result of the function
|
|
172
|
+
*/
|
|
173
|
+
group(label, fn, collapsed = false) {
|
|
174
|
+
this.startGroup(label, collapsed);
|
|
175
|
+
try {
|
|
176
|
+
return fn();
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
this.endGroup();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Execute an async function within a console group
|
|
184
|
+
* @param label Label for the group
|
|
185
|
+
* @param fn Async function to execute
|
|
186
|
+
* @param collapsed Whether the group should be collapsed
|
|
187
|
+
* @returns Promise resolving to the result of the function
|
|
188
|
+
*/
|
|
189
|
+
async groupAsync(label, fn, collapsed = false) {
|
|
190
|
+
this.startGroup(label, collapsed);
|
|
191
|
+
try {
|
|
192
|
+
return await fn();
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
this.endGroup();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Measure the execution time of a function
|
|
200
|
+
* @param label Label for the timing
|
|
201
|
+
* @param fn Function to measure
|
|
202
|
+
* @returns Object containing the result and duration
|
|
203
|
+
*/
|
|
204
|
+
time(label, fn) {
|
|
205
|
+
const start = performance.now();
|
|
206
|
+
const result = fn();
|
|
207
|
+
const duration = performance.now() - start;
|
|
208
|
+
this.info(`${label} took ${duration.toFixed(2)}ms`);
|
|
209
|
+
return { result, duration };
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Measure the execution time of an async function
|
|
213
|
+
* @param label Label for the timing
|
|
214
|
+
* @param fn Async function to measure
|
|
215
|
+
* @returns Promise resolving to an object containing the result and duration
|
|
216
|
+
*/
|
|
217
|
+
async timeAsync(label, fn) {
|
|
218
|
+
const start = performance.now();
|
|
219
|
+
const result = await fn();
|
|
220
|
+
const duration = performance.now() - start;
|
|
221
|
+
this.info(`${label} took ${duration.toFixed(2)}ms`);
|
|
222
|
+
return { result, duration };
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Log with automatic level adjustment based on context depth
|
|
226
|
+
* @param message Message to log
|
|
227
|
+
* @param args Additional arguments to log
|
|
228
|
+
*/
|
|
229
|
+
logWithContext(message, ...args) {
|
|
230
|
+
// Adjust level based on nesting - deeper nesting means more detailed level
|
|
231
|
+
const adjustedLevel = Math.min(LogLevel.VERBOSE, LogLevel.INFO + this.contextDepth);
|
|
232
|
+
this.log(adjustedLevel, message, ...args);
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Main logging method
|
|
236
|
+
* @param level Log level
|
|
237
|
+
* @param message Message to log
|
|
238
|
+
* @param args Additional arguments to log
|
|
239
|
+
*/
|
|
240
|
+
log(level, message, ...args) {
|
|
241
|
+
if (!this.shouldLog(level))
|
|
242
|
+
return;
|
|
243
|
+
const formattedMessage = this.formatMessage(level, message);
|
|
244
|
+
switch (level) {
|
|
245
|
+
case LogLevel.ERROR:
|
|
246
|
+
console.error(formattedMessage, ...args);
|
|
247
|
+
break;
|
|
248
|
+
case LogLevel.WARN:
|
|
249
|
+
console.warn(formattedMessage, ...args);
|
|
250
|
+
break;
|
|
251
|
+
default:
|
|
252
|
+
console.log(formattedMessage, ...args);
|
|
253
|
+
break;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Log an error message
|
|
258
|
+
* @param message Error message
|
|
259
|
+
* @param args Additional arguments to log
|
|
260
|
+
*/
|
|
261
|
+
error(message, ...args) {
|
|
262
|
+
this.log(LogLevel.ERROR, message, ...args);
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Log a warning message
|
|
266
|
+
* @param message Warning message
|
|
267
|
+
* @param args Additional arguments to log
|
|
268
|
+
*/
|
|
269
|
+
warn(message, ...args) {
|
|
270
|
+
this.log(LogLevel.WARN, message, ...args);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Log an info message
|
|
274
|
+
* @param message Info message
|
|
275
|
+
* @param args Additional arguments to log
|
|
276
|
+
*/
|
|
277
|
+
info(message, ...args) {
|
|
278
|
+
this.log(LogLevel.INFO, message, ...args);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Log a debug message
|
|
282
|
+
* @param message Debug message
|
|
283
|
+
* @param args Additional arguments to log
|
|
284
|
+
*/
|
|
285
|
+
debug(message, ...args) {
|
|
286
|
+
this.log(LogLevel.DEBUG, message, ...args);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Log a trace message
|
|
290
|
+
* @param message Trace message
|
|
291
|
+
* @param args Additional arguments to log
|
|
292
|
+
*/
|
|
293
|
+
trace(message, ...args) {
|
|
294
|
+
this.log(LogLevel.TRACE, message, ...args);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Log a verbose message
|
|
298
|
+
* @param message Verbose message
|
|
299
|
+
* @param args Additional arguments to log
|
|
300
|
+
*/
|
|
301
|
+
verbose(message, ...args) {
|
|
302
|
+
this.log(LogLevel.VERBOSE, message, ...args);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Check if a message should be logged
|
|
306
|
+
* @param level Log level of the message
|
|
307
|
+
* @returns Whether the message should be logged
|
|
308
|
+
*/
|
|
309
|
+
shouldLog(level) {
|
|
310
|
+
// Always log errors and warnings, otherwise respect enabled flag
|
|
311
|
+
if (!this.enabled && level > LogLevel.WARN)
|
|
312
|
+
return false;
|
|
313
|
+
// If we're focusing on a specific level, only log messages at that level
|
|
314
|
+
if (this.focusedLevel !== null) {
|
|
315
|
+
return level === this.focusedLevel || level <= LogLevel.WARN;
|
|
316
|
+
}
|
|
317
|
+
// Otherwise, respect the level threshold
|
|
318
|
+
if (level > this.currentLevel && level > LogLevel.WARN)
|
|
319
|
+
return false;
|
|
320
|
+
// Check if component is enabled
|
|
321
|
+
if (this.enabledComponents !== null && !this.enabledComponents.has(this.componentName)) {
|
|
322
|
+
// Still allow errors and warnings
|
|
323
|
+
return level <= LogLevel.WARN;
|
|
324
|
+
}
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Format a log message
|
|
329
|
+
* @param level Log level
|
|
330
|
+
* @param message Message to format
|
|
331
|
+
* @returns Formatted message
|
|
332
|
+
*/
|
|
333
|
+
formatMessage(level, message) {
|
|
334
|
+
const parts = [];
|
|
335
|
+
// Add timestamp if enabled
|
|
336
|
+
if (this.includeTimestamps) {
|
|
337
|
+
parts.push(this.getTimestamp());
|
|
338
|
+
}
|
|
339
|
+
// Add level prefix
|
|
340
|
+
parts.push(this.getLevelPrefix(level));
|
|
341
|
+
// Add component name if enabled
|
|
342
|
+
if (this.includeComponentName) {
|
|
343
|
+
parts.push(this.componentName);
|
|
344
|
+
}
|
|
345
|
+
// Add indentation based on context depth
|
|
346
|
+
const indentation = " ".repeat(this.contextDepth);
|
|
347
|
+
// Combine all parts
|
|
348
|
+
return `${parts.join(" ")}: ${indentation}${message}`;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Get the current timestamp as a string
|
|
352
|
+
* @returns Formatted timestamp
|
|
353
|
+
*/
|
|
354
|
+
getTimestamp() {
|
|
355
|
+
const now = new Date();
|
|
356
|
+
return `[${now.toISOString()}]`;
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Get prefix for log level
|
|
360
|
+
* @param level Log level
|
|
361
|
+
* @returns Formatted prefix
|
|
362
|
+
*/
|
|
363
|
+
getLevelPrefix(level) {
|
|
364
|
+
if (!this.useColors) {
|
|
365
|
+
return `[${getLogLevelName(level)}]`;
|
|
366
|
+
}
|
|
367
|
+
// Use colors for different levels
|
|
368
|
+
switch (level) {
|
|
369
|
+
case LogLevel.ERROR:
|
|
370
|
+
return "\x1b[31m[ERROR]\x1b[0m"; // Red
|
|
371
|
+
case LogLevel.WARN:
|
|
372
|
+
return "\x1b[33m[WARN]\x1b[0m"; // Yellow
|
|
373
|
+
case LogLevel.INFO:
|
|
374
|
+
return "\x1b[36m[INFO]\x1b[0m"; // Cyan
|
|
375
|
+
case LogLevel.DEBUG:
|
|
376
|
+
return "\x1b[32m[DEBUG]\x1b[0m"; // Green
|
|
377
|
+
case LogLevel.TRACE:
|
|
378
|
+
return "\x1b[35m[TRACE]\x1b[0m"; // Magenta
|
|
379
|
+
case LogLevel.VERBOSE:
|
|
380
|
+
return "\x1b[90m[VERBOSE]\x1b[0m"; // Gray
|
|
381
|
+
default:
|
|
382
|
+
return `[${getLogLevelName(level)}]`;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for components that support logging
|
|
3
|
+
*/
|
|
4
|
+
export interface Loggable {
|
|
5
|
+
/**
|
|
6
|
+
* Log a debug message if debug mode is enabled
|
|
7
|
+
* @param message Message to log
|
|
8
|
+
* @param args Additional arguments to log
|
|
9
|
+
*/
|
|
10
|
+
log(message: string, ...args: unknown[]): void;
|
|
11
|
+
/**
|
|
12
|
+
* Log a warning message
|
|
13
|
+
* @param message Warning message
|
|
14
|
+
* @param args Additional arguments to log
|
|
15
|
+
*/
|
|
16
|
+
warn(message: string, ...args: unknown[]): void;
|
|
17
|
+
/**
|
|
18
|
+
* Log an error message
|
|
19
|
+
* @param message Error message
|
|
20
|
+
* @param args Additional arguments to log
|
|
21
|
+
*/
|
|
22
|
+
error(message: string, ...args: unknown[]): void;
|
|
23
|
+
/**
|
|
24
|
+
* Set debug mode
|
|
25
|
+
* @param enabled Whether debug mode should be enabled
|
|
26
|
+
*/
|
|
27
|
+
setDebugMode(enabled: boolean): void;
|
|
28
|
+
/**
|
|
29
|
+
* Check if debug mode is enabled
|
|
30
|
+
* @returns Whether debug mode is enabled
|
|
31
|
+
*/
|
|
32
|
+
isDebugEnabled(): boolean;
|
|
33
|
+
}
|