-
Notifications
You must be signed in to change notification settings - Fork 4
consoleCatcher init #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
consoleCatcher init #116
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2a843cd
consoleCatcher init
Dobrunia 36dd061
fix
Dobrunia 04d76e7
fix
Dobrunia f1e6031
fix
Dobrunia ca73ec9
fix
Dobrunia 0c9a73f
fix
Dobrunia 484f0bc
fix
Dobrunia 545ba8b
fix
Dobrunia 286f16f
fix
Dobrunia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,80 @@ | ||
| /** | ||
| * @file Integration for catching console logs and other info | ||
| * @file Module for intercepting console logs with stack trace capture | ||
| */ | ||
|
|
||
| interface ConsoleLogEvent { | ||
| /** | ||
| * Window.console object method (i.e. log, info, warn) | ||
| */ | ||
| method: string; | ||
|
|
||
| /** | ||
| * Time when the log was occurred | ||
| */ | ||
| timestamp: Date; | ||
|
|
||
| /** | ||
| * Log argument | ||
| */ | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| args: any; | ||
| } | ||
| import type { ConsoleLogEvent } from '@hawk.so/types/build/src/base/event/addons/javascript'; | ||
|
|
||
| const MAX_LOGS = 20; | ||
| const consoleOutput: ConsoleLogEvent[] = []; | ||
|
||
|
|
||
| let isInitialized = false; | ||
|
|
||
| /** | ||
| * Contains all data that will be logged by window.console | ||
| * Initializes the console catcher by overriding console methods | ||
| * to capture logs with stack traces. | ||
| */ | ||
| const consoleOutput: ConsoleLogEvent[] = []; | ||
| export function initConsoleCatcher(): void { | ||
| if (isInitialized) { | ||
| return; | ||
| }; | ||
|
|
||
| isInitialized = true; | ||
| const consoleMethods = ['log', 'warn', 'error', 'info', 'debug']; | ||
|
|
||
| consoleMethods.forEach((method) => { | ||
| if (typeof window.console[method] !== 'function') { | ||
| return; | ||
| }; | ||
|
|
||
| // Override console methods | ||
| Object.keys(window.console).forEach(key => { | ||
| const oldFunction = window.console[key]; | ||
| const oldFunction = window.console[method].bind(window.console); | ||
|
|
||
| window.console[key] = function (...args): void { | ||
| consoleOutput.push({ | ||
| method: key, | ||
| window.console[method] = function (...args): void { | ||
| if (consoleOutput.length >= MAX_LOGS) { | ||
| consoleOutput.shift(); | ||
| } | ||
|
|
||
| const stack = new Error().stack?.split('\n').slice(2).join('\n') || ''; | ||
|
|
||
| const logEvent: ConsoleLogEvent = { | ||
| method, | ||
| timestamp: new Date(), | ||
| type: method, | ||
| message: args | ||
| .map((arg) => (typeof arg === 'string' ? arg : JSON.stringify(arg))).join(' '), | ||
| stack, | ||
| fileLine: stack.split('\n')[0]?.trim(), | ||
| }; | ||
|
|
||
| consoleOutput.push(logEvent); | ||
| oldFunction(...args); | ||
| }; | ||
| }); | ||
|
|
||
| window.addEventListener('error', function (event) { | ||
|
||
| if (consoleOutput.length >= MAX_LOGS) { | ||
| consoleOutput.shift(); | ||
| } | ||
|
|
||
| const logEvent: ConsoleLogEvent = { | ||
| method: 'error', | ||
| timestamp: new Date(), | ||
| args, | ||
| }); | ||
| oldFunction.apply(window.console, args); | ||
| }; | ||
| }); | ||
| type: event.error?.name || 'Error', | ||
| message: event.error?.message || event.message, | ||
| stack: event.error?.stack || '', | ||
| fileLine: event.filename | ||
| ? `${event.filename}:${event.lineno}:${event.colno}` | ||
| : '', | ||
| }; | ||
|
|
||
| consoleOutput.push(logEvent); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * @param event - event to modify | ||
| * @param data - event data | ||
| * Returns the stack of captured console logs. | ||
| * | ||
| * @returns {ConsoleLogEvent[]} Array of logged console events. | ||
| */ | ||
| export default function (event, data): void { | ||
| data.payload.consoleOutput = consoleOutput; | ||
| export function getConsoleLogStack(): ConsoleLogEvent[] { | ||
| return [ ...consoleOutput ]; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.