|
1 | 1 | /** |
2 | | - * @file Integration for catching console logs and other info |
| 2 | + * @file Module for intercepting console logs with stack trace capture |
3 | 3 | */ |
4 | 4 |
|
5 | | -interface ConsoleLogEvent { |
6 | | - /** |
7 | | - * Window.console object method (i.e. log, info, warn) |
8 | | - */ |
9 | | - method: string; |
| 5 | +import type { ConsoleLogEvent } from '@hawk.so/types'; |
10 | 6 |
|
11 | | - /** |
12 | | - * Time when the log was occurred |
13 | | - */ |
14 | | - timestamp: Date; |
| 7 | +const createConsoleCatcher = (): { |
| 8 | + initConsoleCatcher: () => void; |
| 9 | + addErrorEvent: (event: ErrorEvent | PromiseRejectionEvent) => void; |
| 10 | + getConsoleLogStack: () => ConsoleLogEvent[]; |
| 11 | +} => { |
| 12 | + const MAX_LOGS = 20; |
| 13 | + const consoleOutput: ConsoleLogEvent[] = []; |
| 14 | + let isInitialized = false; |
15 | 15 |
|
16 | | - /** |
17 | | - * Log argument |
18 | | - */ |
19 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
20 | | - args: any; |
21 | | -} |
22 | | - |
23 | | -/** |
24 | | - * Contains all data that will be logged by window.console |
25 | | - */ |
26 | | -const consoleOutput: ConsoleLogEvent[] = []; |
| 16 | + const addToConsoleOutput = (logEvent: ConsoleLogEvent): void => { |
| 17 | + if (consoleOutput.length >= MAX_LOGS) { |
| 18 | + consoleOutput.shift(); |
| 19 | + } |
| 20 | + consoleOutput.push(logEvent); |
| 21 | + }; |
27 | 22 |
|
28 | | -// Override console methods |
29 | | -Object.keys(window.console).forEach(key => { |
30 | | - const oldFunction = window.console[key]; |
| 23 | + const createConsoleEventFromError = ( |
| 24 | + event: ErrorEvent | PromiseRejectionEvent |
| 25 | + ): ConsoleLogEvent => { |
| 26 | + if (event instanceof ErrorEvent) { |
| 27 | + return { |
| 28 | + method: 'error', |
| 29 | + timestamp: new Date(), |
| 30 | + type: event.error?.name || 'Error', |
| 31 | + message: event.error?.message || event.message, |
| 32 | + stack: event.error?.stack || '', |
| 33 | + fileLine: event.filename |
| 34 | + ? `${event.filename}:${event.lineno}:${event.colno}` |
| 35 | + : '', |
| 36 | + }; |
| 37 | + } |
31 | 38 |
|
32 | | - window.console[key] = function (...args): void { |
33 | | - consoleOutput.push({ |
34 | | - method: key, |
| 39 | + return { |
| 40 | + method: 'error', |
35 | 41 | timestamp: new Date(), |
36 | | - args, |
37 | | - }); |
38 | | - oldFunction.apply(window.console, args); |
| 42 | + type: 'UnhandledRejection', |
| 43 | + message: event.reason?.message || String(event.reason), |
| 44 | + stack: event.reason?.stack || '', |
| 45 | + fileLine: '', |
| 46 | + }; |
39 | 47 | }; |
40 | | -}); |
41 | 48 |
|
42 | | -/** |
43 | | - * @param event - event to modify |
44 | | - * @param data - event data |
45 | | - */ |
46 | | -export default function (event, data): void { |
47 | | - data.payload.consoleOutput = consoleOutput; |
48 | | -} |
| 49 | + return { |
| 50 | + initConsoleCatcher(): void { |
| 51 | + if (isInitialized) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + isInitialized = true; |
| 56 | + const consoleMethods: string[] = ['log', 'warn', 'error', 'info', 'debug']; |
| 57 | + |
| 58 | + consoleMethods.forEach((method) => { |
| 59 | + if (typeof window.console[method] !== 'function') { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const oldFunction = window.console[method].bind(window.console); |
| 64 | + |
| 65 | + window.console[method] = function (...args: unknown[]): void { |
| 66 | + const stack = new Error().stack?.split('\n').slice(2).join('\n') || ''; |
| 67 | + |
| 68 | + const logEvent: ConsoleLogEvent = { |
| 69 | + method, |
| 70 | + timestamp: new Date(), |
| 71 | + type: method, |
| 72 | + message: args.map((arg) => typeof arg === 'string' ? arg : JSON.stringify(arg)).join(' '), |
| 73 | + stack, |
| 74 | + fileLine: stack.split('\n')[0]?.trim(), |
| 75 | + }; |
| 76 | + |
| 77 | + addToConsoleOutput(logEvent); |
| 78 | + oldFunction(...args); |
| 79 | + }; |
| 80 | + }); |
| 81 | + }, |
| 82 | + |
| 83 | + addErrorEvent(event: ErrorEvent | PromiseRejectionEvent): void { |
| 84 | + const logEvent = createConsoleEventFromError(event); |
| 85 | + |
| 86 | + addToConsoleOutput(logEvent); |
| 87 | + }, |
| 88 | + |
| 89 | + getConsoleLogStack(): ConsoleLogEvent[] { |
| 90 | + return [ ...consoleOutput ]; |
| 91 | + }, |
| 92 | + }; |
| 93 | +}; |
| 94 | + |
| 95 | +const consoleCatcher = createConsoleCatcher(); |
| 96 | +export const { initConsoleCatcher, getConsoleLogStack, addErrorEvent } = consoleCatcher; |
0 commit comments