22 * @file Module for intercepting console logs with stack trace capture
33 */
44import safeStringify from 'safe-stringify' ;
5- import type { ConsoleLogEvent } from '@hawk.so /types' ;
5+ import type { ExtendedConsoleLogEvent as ConsoleLogEvent } from '.. /types/console ' ;
66
77/**
8- * Creates a console interceptor that captures and formats console output
8+ * Console interceptor that captures and formats console output
99 */
10- function createConsoleCatcher ( ) : {
11- initConsoleCatcher : ( ) => void ;
12- addErrorEvent : ( event : ErrorEvent | PromiseRejectionEvent ) => void ;
13- getConsoleLogStack : ( ) => ConsoleLogEvent [ ] ;
14- } {
15- const MAX_LOGS = 20 ;
16- const consoleOutput : ConsoleLogEvent [ ] = [ ] ;
17- let isInitialized = false ;
10+ export class ConsoleCatcher {
11+ private readonly MAX_LOGS = 20 ;
12+ private readonly consoleOutput : ConsoleLogEvent [ ] = [ ] ;
13+ private isInitialized = false ;
1814
1915 /**
2016 * Converts any argument to its string representation
21- *
22- * @param arg - Value to convert to string
2317 */
24- function stringifyArg ( arg : unknown ) : string {
18+ private stringifyArg ( arg : unknown ) : string {
2519 if ( typeof arg === 'string' ) {
2620 return arg ;
2721 }
@@ -34,10 +28,8 @@ function createConsoleCatcher(): {
3428
3529 /**
3630 * Formats console arguments handling %c directives
37- *
38- * @param args - Console arguments that may include style directives
3931 */
40- function formatConsoleArgs ( args : unknown [ ] ) : {
32+ private formatConsoleArgs ( args : unknown [ ] ) : {
4133 message : string ;
4234 styles : string [ ] ;
4335 } {
@@ -52,7 +44,7 @@ function createConsoleCatcher(): {
5244
5345 if ( typeof firstArg !== 'string' || ! firstArg . includes ( '%c' ) ) {
5446 return {
55- message : args . map ( stringifyArg ) . join ( ' ' ) ,
47+ message : args . map ( ( arg ) => this . stringifyArg ( arg ) ) . join ( ' ' ) ,
5648 styles : [ ] ,
5749 } ;
5850 }
@@ -76,7 +68,7 @@ function createConsoleCatcher(): {
7668 // Add remaining arguments that aren't styles
7769 const remainingArgs = args
7870 . slice ( styles . length + 1 )
79- . map ( stringifyArg )
71+ . map ( ( arg ) => this . stringifyArg ( arg ) )
8072 . join ( ' ' ) ;
8173
8274 return {
@@ -87,34 +79,26 @@ function createConsoleCatcher(): {
8779
8880 /**
8981 * Adds a console log event to the output buffer
90- *
91- * @param logEvent - The console log event to be added to the output buffer
9282 */
93- function addToConsoleOutput ( logEvent : ConsoleLogEvent ) : void {
94- if ( consoleOutput . length >= MAX_LOGS ) {
95- consoleOutput . shift ( ) ;
83+ private addToConsoleOutput ( logEvent : ConsoleLogEvent ) : void {
84+ if ( this . consoleOutput . length >= this . MAX_LOGS ) {
85+ this . consoleOutput . shift ( ) ;
9686 }
97- consoleOutput . push ( logEvent ) ;
87+ this . consoleOutput . push ( logEvent ) ;
9888 }
9989
10090 /**
10191 * Creates a console log event from an error or promise rejection
102- *
103- * @param event - The error event or promise rejection event to convert
10492 */
105- function createConsoleEventFromError (
106- event : ErrorEvent | PromiseRejectionEvent
107- ) : ConsoleLogEvent {
93+ private createConsoleEventFromError ( event : ErrorEvent | PromiseRejectionEvent ) : ConsoleLogEvent {
10894 if ( event instanceof ErrorEvent ) {
10995 return {
11096 method : 'error' ,
11197 timestamp : new Date ( ) ,
11298 type : event . error ?. name || 'Error' ,
11399 message : event . error ?. message || event . message ,
114100 stack : event . error ?. stack || '' ,
115- fileLine : event . filename
116- ? `${ event . filename } :${ event . lineno } :${ event . colno } `
117- : '' ,
101+ fileLine : event . filename ? `${ event . filename } :${ event . lineno } :${ event . colno } ` : '' ,
118102 } ;
119103 }
120104
@@ -131,23 +115,22 @@ function createConsoleCatcher(): {
131115 /**
132116 * Initializes the console interceptor by overriding default console methods
133117 */
134- function initConsoleCatcher ( ) : void {
135- if ( isInitialized ) {
118+ public init ( ) : void {
119+ if ( this . isInitialized ) {
136120 return ;
137121 }
138122
139- isInitialized = true ;
123+ this . isInitialized = true ;
140124 const consoleMethods : string [ ] = [ 'log' , 'warn' , 'error' , 'info' , 'debug' ] ;
141125
142- consoleMethods . forEach ( function overrideConsoleMethod ( method ) {
126+ consoleMethods . forEach ( ( method ) => {
143127 if ( typeof window . console [ method ] !== 'function' ) {
144128 return ;
145129 }
146130
147131 const oldFunction = window . console [ method ] . bind ( window . console ) ;
148132
149- window . console [ method ] = function ( ...args : unknown [ ] ) : void {
150-
133+ window . console [ method ] = ( ...args : unknown [ ] ) : void => {
151134 /**
152135 * If the console call originates from Vue's internal runtime bundle, skip interception
153136 * to avoid capturing Vue-internal warnings and causing recursive loops.
@@ -157,9 +140,8 @@ function createConsoleCatcher(): {
157140 return oldFunction ( ...args ) ;
158141 }
159142
160- const stack = new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 )
161- . join ( '\n' ) || '' ;
162- const { message, styles } = formatConsoleArgs ( args ) ;
143+ const stack = new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 ) . join ( '\n' ) || '' ;
144+ const { message, styles } = this . formatConsoleArgs ( args ) ;
163145
164146 const logEvent : ConsoleLogEvent = {
165147 method,
@@ -171,38 +153,24 @@ function createConsoleCatcher(): {
171153 styles,
172154 } ;
173155
174- addToConsoleOutput ( logEvent ) ;
156+ this . addToConsoleOutput ( logEvent ) ;
175157 oldFunction ( ...args ) ;
176158 } ;
177159 } ) ;
178160 }
179161
180162 /**
181163 * Handles error events by converting them to console log events
182- *
183- * @param event - The error or promise rejection event to handle
184164 */
185- function addErrorEvent ( event : ErrorEvent | PromiseRejectionEvent ) : void {
186- const logEvent = createConsoleEventFromError ( event ) ;
187-
188- addToConsoleOutput ( logEvent ) ;
165+ public addErrorEvent ( event : ErrorEvent | PromiseRejectionEvent ) : void {
166+ const logEvent = this . createConsoleEventFromError ( event ) ;
167+ this . addToConsoleOutput ( logEvent ) ;
189168 }
190169
191170 /**
192171 * Returns the current console output buffer
193172 */
194- function getConsoleLogStack ( ) : ConsoleLogEvent [ ] {
195- return [ ...consoleOutput ] ;
173+ public getConsoleLogStack ( ) : ConsoleLogEvent [ ] {
174+ return [ ...this . consoleOutput ] ;
196175 }
197-
198- return {
199- initConsoleCatcher,
200- addErrorEvent,
201- getConsoleLogStack,
202- } ;
203176}
204-
205- const consoleCatcher = createConsoleCatcher ( ) ;
206-
207- export const { initConsoleCatcher, getConsoleLogStack, addErrorEvent } =
208- consoleCatcher ;
0 commit comments