|
1 | 1 | import { ErrorLevel } from "./errorLevel";
|
2 | 2 | import { QuickAddLogger } from "./quickAddLogger";
|
3 | 3 | import type { QuickAddError } from "./quickAddError";
|
| 4 | +import { MAX_ERROR_LOG_SIZE } from "../utils/errorUtils"; |
4 | 5 |
|
| 6 | +/** |
| 7 | + * Logger implementation that outputs to the browser console and maintains an error log |
| 8 | + * with a maximum size to prevent memory leaks. Uses native Error objects to leverage |
| 9 | + * browser DevTools stack trace display. |
| 10 | + */ |
5 | 11 | export class ConsoleErrorLogger extends QuickAddLogger {
|
| 12 | + /** |
| 13 | + * In-memory log of errors for debugging |
| 14 | + * Limited to MAX_ERROR_LOG_SIZE entries to prevent memory leaks |
| 15 | + */ |
6 | 16 | public ErrorLog: QuickAddError[] = [];
|
7 | 17 |
|
8 |
| - public logError(errorMsg: string) { |
9 |
| - const error = this.getQuickAddError(errorMsg, ErrorLevel.Error); |
| 18 | + /** |
| 19 | + * Logs an error to the console with proper stack trace handling |
| 20 | + * |
| 21 | + * @param errorMsg - Error message or Error object |
| 22 | + * @param stack - Optional stack trace string |
| 23 | + * @param originalError - Optional original Error object |
| 24 | + */ |
| 25 | + public logError(errorMsg: string, stack?: string, originalError?: Error) { |
| 26 | + const error = this.getQuickAddError(errorMsg, ErrorLevel.Error, stack, originalError); |
10 | 27 | this.addMessageToErrorLog(error);
|
11 | 28 |
|
12 |
| - console.error(this.formatOutputString(error)); |
| 29 | + // Always pass the original error or create a new one to leverage Dev Tools' stack trace UI |
| 30 | + const errorToLog = originalError || new Error(errorMsg); |
| 31 | + |
| 32 | + // Just log the message as the first argument and the error object as the second |
| 33 | + console.error(this.formatOutputString(error), errorToLog); |
13 | 34 | }
|
14 | 35 |
|
15 |
| - public logWarning(warningMsg: string) { |
16 |
| - const warning = this.getQuickAddError(warningMsg, ErrorLevel.Warning); |
| 36 | + /** |
| 37 | + * Logs a warning to the console with proper stack trace handling |
| 38 | + * |
| 39 | + * @param warningMsg - Warning message or Error object |
| 40 | + * @param stack - Optional stack trace string |
| 41 | + * @param originalError - Optional original Error object |
| 42 | + */ |
| 43 | + public logWarning(warningMsg: string, stack?: string, originalError?: Error) { |
| 44 | + const warning = this.getQuickAddError(warningMsg, ErrorLevel.Warning, stack, originalError); |
17 | 45 | this.addMessageToErrorLog(warning);
|
18 | 46 |
|
19 |
| - console.warn(this.formatOutputString(warning)); |
| 47 | + // Always pass the original error or create a new one to leverage Dev Tools' stack trace UI |
| 48 | + const errorToLog = originalError || new Error(warningMsg); |
| 49 | + |
| 50 | + console.warn(this.formatOutputString(warning), errorToLog); |
20 | 51 | }
|
21 | 52 |
|
22 |
| - public logMessage(logMsg: string) { |
23 |
| - const log = this.getQuickAddError(logMsg, ErrorLevel.Log); |
| 53 | + /** |
| 54 | + * Logs a message to the console |
| 55 | + * |
| 56 | + * @param logMsg - Log message |
| 57 | + * @param stack - Optional stack trace string |
| 58 | + * @param originalError - Optional original Error object |
| 59 | + */ |
| 60 | + public logMessage(logMsg: string, stack?: string, originalError?: Error) { |
| 61 | + const log = this.getQuickAddError(logMsg, ErrorLevel.Log, stack, originalError); |
24 | 62 | this.addMessageToErrorLog(log);
|
25 | 63 |
|
26 |
| - console.log(this.formatOutputString(log)); |
| 64 | + // For regular logs, we'll still show the error if available |
| 65 | + if (originalError) { |
| 66 | + console.log(this.formatOutputString(log), originalError); |
| 67 | + } else { |
| 68 | + console.log(this.formatOutputString(log)); |
| 69 | + } |
27 | 70 | }
|
28 | 71 |
|
| 72 | + /** |
| 73 | + * Adds an error to the error log, maintaining the maximum size limit |
| 74 | + * by removing the oldest entries when needed |
| 75 | + * |
| 76 | + * @param error - Error to add to the log |
| 77 | + */ |
29 | 78 | private addMessageToErrorLog(error: QuickAddError): void {
|
| 79 | + // Add the new error |
30 | 80 | this.ErrorLog.push(error);
|
| 81 | + |
| 82 | + // If we've exceeded the maximum size, remove the oldest entries |
| 83 | + if (this.ErrorLog.length > MAX_ERROR_LOG_SIZE) { |
| 84 | + this.ErrorLog = this.ErrorLog.slice(-MAX_ERROR_LOG_SIZE); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Clears the error log |
| 90 | + */ |
| 91 | + public clearErrorLog(): void { |
| 92 | + this.ErrorLog = []; |
31 | 93 | }
|
32 | 94 | }
|
0 commit comments