11/**
22 * @file Module for intercepting console logs with stack trace capture
33 */
4- import safeStringify from " safe-stringify" ;
5- import type { ConsoleLogEvent } from " @hawk.so/types" ;
4+ import safeStringify from ' safe-stringify' ;
5+ import type { ConsoleLogEvent } from ' @hawk.so/types' ;
66
77const createConsoleCatcher = ( ) : {
88 initConsoleCatcher : ( ) => void ;
@@ -19,14 +19,14 @@ const createConsoleCatcher = (): {
1919 const formatConsoleArgs = (
2020 args : unknown [ ]
2121 ) : { message : string ; styles : string [ ] } => {
22- if ( args . length === 0 ) return { message : "" , styles : [ ] } ;
22+ if ( args . length === 0 ) return { message : '' , styles : [ ] } ;
2323
2424 const firstArg = args [ 0 ] ;
25- if ( typeof firstArg !== " string" || ! firstArg . includes ( "%c" ) ) {
25+ if ( typeof firstArg !== ' string' || ! firstArg . includes ( '%c' ) ) {
2626 return {
2727 message : args
28- . map ( ( arg ) => ( typeof arg === " string" ? arg : safeStringify ( arg ) ) )
29- . join ( " " ) ,
28+ . map ( ( arg ) => ( typeof arg === ' string' ? arg : safeStringify ( arg ) ) )
29+ . join ( ' ' ) ,
3030 styles : [ ] ,
3131 } ;
3232 }
@@ -41,20 +41,20 @@ const createConsoleCatcher = (): {
4141 for ( let i = 1 ; i < args . length ; i ++ ) {
4242 const arg = args [ i ] ;
4343
44- if ( typeof arg === " string" && message . indexOf ( "%c" , styleIndex ) !== - 1 ) {
44+ if ( typeof arg === ' string' && message . indexOf ( '%c' , styleIndex ) !== - 1 ) {
4545 styles . push ( arg ) ;
46- styleIndex = message . indexOf ( "%c" , styleIndex ) + 2 ;
46+ styleIndex = message . indexOf ( '%c' , styleIndex ) + 2 ;
4747 }
4848 }
4949
5050 // Add remaining arguments that aren't styles
5151 const remainingArgs = args
5252 . slice ( styles . length + 1 )
53- . map ( ( arg ) => ( typeof arg === " string" ? arg : safeStringify ( arg ) ) )
54- . join ( " " ) ;
53+ . map ( ( arg ) => ( typeof arg === ' string' ? arg : safeStringify ( arg ) ) )
54+ . join ( ' ' ) ;
5555
5656 return {
57- message : message + ( remainingArgs ? " " + remainingArgs : "" ) ,
57+ message : message + ( remainingArgs ? ' ' + remainingArgs : '' ) ,
5858 styles,
5959 } ;
6060 } ;
@@ -71,24 +71,24 @@ const createConsoleCatcher = (): {
7171 ) : ConsoleLogEvent => {
7272 if ( event instanceof ErrorEvent ) {
7373 return {
74- method : " error" ,
74+ method : ' error' ,
7575 timestamp : new Date ( ) ,
76- type : event . error ?. name || " Error" ,
76+ type : event . error ?. name || ' Error' ,
7777 message : event . error ?. message || event . message ,
78- stack : event . error ?. stack || "" ,
78+ stack : event . error ?. stack || '' ,
7979 fileLine : event . filename
8080 ? `${ event . filename } :${ event . lineno } :${ event . colno } `
81- : "" ,
81+ : '' ,
8282 } ;
8383 }
8484
8585 return {
86- method : " error" ,
86+ method : ' error' ,
8787 timestamp : new Date ( ) ,
88- type : " UnhandledRejection" ,
88+ type : ' UnhandledRejection' ,
8989 message : event . reason ?. message || String ( event . reason ) ,
90- stack : event . reason ?. stack || "" ,
91- fileLine : "" ,
90+ stack : event . reason ?. stack || '' ,
91+ fileLine : '' ,
9292 } ;
9393 } ;
9494
@@ -100,23 +100,23 @@ const createConsoleCatcher = (): {
100100
101101 isInitialized = true ;
102102 const consoleMethods : string [ ] = [
103- " log" ,
104- " warn" ,
105- " error" ,
106- " info" ,
107- " debug" ,
103+ ' log' ,
104+ ' warn' ,
105+ ' error' ,
106+ ' info' ,
107+ ' debug' ,
108108 ] ;
109109
110110 consoleMethods . forEach ( ( method ) => {
111- if ( typeof window . console [ method ] !== " function" ) {
111+ if ( typeof window . console [ method ] !== ' function' ) {
112112 return ;
113113 }
114114
115115 const oldFunction = window . console [ method ] . bind ( window . console ) ;
116116
117117 window . console [ method ] = function ( ...args : unknown [ ] ) : void {
118118 const stack =
119- new Error ( ) . stack ?. split ( "\n" ) . slice ( 2 ) . join ( "\n" ) || "" ;
119+ new Error ( ) . stack ?. split ( '\n' ) . slice ( 2 ) . join ( '\n' ) || '' ;
120120 const { message, styles } = formatConsoleArgs ( args ) ;
121121
122122 const logEvent : ConsoleLogEvent = {
@@ -125,7 +125,7 @@ const createConsoleCatcher = (): {
125125 type : method ,
126126 message,
127127 stack,
128- fileLine : stack . split ( "\n" ) [ 0 ] ?. trim ( ) ,
128+ fileLine : stack . split ( '\n' ) [ 0 ] ?. trim ( ) ,
129129 styles,
130130 } ;
131131
0 commit comments