22 * @file Module for intercepting console logs with stack trace capture
33 */
44
5- import { ConsoleLogEvent } from " @hawk.so/types/build/src/base/event/addons/javascript" ;
5+ import type { ConsoleLogEvent } from ' @hawk.so/types/build/src/base/event/addons/javascript' ;
66
77const MAX_LOGS = 20 ;
88const consoleOutput : ConsoleLogEvent [ ] = [ ] ;
99
1010let isInitialized = false ;
1111
1212export function initConsoleCatcher ( ) : void {
13- if ( isInitialized ) return ;
13+ if ( isInitialized ) { return } ;
1414 isInitialized = true ;
1515
16- const consoleMethods = [ " log" , " warn" , " error" , " info" , " debug" ] ;
16+ const consoleMethods = [ ' log' , ' warn' , ' error' , ' info' , ' debug' ] ;
1717
1818 consoleMethods . forEach ( ( method ) => {
19- if ( typeof window . console [ method ] !== " function" ) return ;
19+ if ( typeof window . console [ method ] !== ' function' ) { return } ;
2020
2121 const oldFunction = window . console [ method ] . bind ( window . console ) ;
2222
@@ -25,38 +25,38 @@ export function initConsoleCatcher(): void {
2525 consoleOutput . shift ( ) ;
2626 }
2727
28- const stack = new Error ( ) . stack ?. split ( "\n" ) . slice ( 2 ) . join ( "\n" ) || "" ;
28+ const stack = new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 ) . join ( '\n' ) || '' ;
2929
3030 const logEvent : ConsoleLogEvent = {
3131 method,
3232 timestamp : new Date ( ) ,
3333 type : method ,
3434 message : args
35- . map ( ( arg ) => ( typeof arg === " string" ? arg : JSON . stringify ( arg ) ) )
35+ . map ( ( arg ) => ( typeof arg === ' string' ? arg : JSON . stringify ( arg ) ) )
3636 . join ( " " ) ,
3737 stack,
38- fileLine : stack . split ( "\n" ) [ 0 ] ?. trim ( ) ,
38+ fileLine : stack . split ( '\n' ) [ 0 ] ?. trim ( ) ,
3939 } ;
4040
4141 consoleOutput . push ( logEvent ) ;
4242 oldFunction ( ...args ) ;
4343 } ;
4444 } ) ;
4545
46- window . addEventListener ( " error" , function ( event ) {
46+ window . addEventListener ( ' error' , function ( event ) {
4747 if ( consoleOutput . length >= MAX_LOGS ) {
4848 consoleOutput . shift ( ) ;
4949 }
5050
5151 const logEvent : ConsoleLogEvent = {
52- method : " error" ,
52+ method : ' error' ,
5353 timestamp : new Date ( ) ,
54- type : event . error ?. name || " Error" ,
54+ type : event . error ?. name || ' Error' ,
5555 message : event . error ?. message || event . message ,
56- stack : event . error ?. stack || "" ,
56+ stack : event . error ?. stack || '' ,
5757 fileLine : event . filename
5858 ? `${ event . filename } :${ event . lineno } :${ event . colno } `
59- : "" ,
59+ : '' ,
6060 } ;
6161
6262 consoleOutput . push ( logEvent ) ;
0 commit comments