@@ -58,7 +58,8 @@ export default class LoggerService {
5858 if ( ! this . checkIsLogRequired ( 'console' , level ) ) return null ;
5959 const refinedMessage = this . refineMessagePerTransport ( 'console' , message ) ;
6060 const consoleFunc = level === 'notice' ? 'log' : level ;
61- return console [ consoleFunc ] ( JSON . stringify ( refinedMessage ) ) ;
61+ const sanitizedMessage = this . sanitizeForJson ( refinedMessage ) ;
62+ return console [ consoleFunc ] ( JSON . stringify ( sanitizedMessage ) ) ;
6263 }
6364 checkIsLogRequired ( transportName , level ) {
6465 const transport = this . getTransportByName ( transportName ) ;
@@ -117,4 +118,54 @@ export default class LoggerService {
117118 transport => transport . logType === transportName
118119 ) ;
119120 }
121+ /**
122+ * Sanitizes an object for JSON serialization by:
123+ * - Replacing functions with a string representation (similar to console.log behavior)
124+ * - Handling circular references
125+ * - Preserving other values
126+ */
127+ sanitizeForJson ( obj , seen = new WeakSet ( ) ) {
128+ // Handle null and undefined
129+ if ( obj === null || obj === undefined ) {
130+ return obj ;
131+ }
132+ // Handle functions - replace with string representation like console.log does
133+ if ( typeof obj === 'function' ) {
134+ const funcName = obj . name || 'anonymous' ;
135+ return `[Function: ${ funcName } ]` ;
136+ }
137+ // Handle primitives
138+ if ( typeof obj !== 'object' ) {
139+ return obj ;
140+ }
141+ // Handle circular references
142+ if ( seen . has ( obj ) ) {
143+ return '[Circular]' ;
144+ }
145+ // Handle Date objects
146+ if ( obj instanceof Date ) {
147+ return obj . toISOString ( ) ;
148+ }
149+ // Handle arrays
150+ if ( Array . isArray ( obj ) ) {
151+ seen . add ( obj ) ;
152+ return obj . map ( item => this . sanitizeForJson ( item , seen ) ) ;
153+ }
154+ // Handle objects
155+ seen . add ( obj ) ;
156+ const sanitized = { } ;
157+ for ( const key in obj ) {
158+ if ( Object . prototype . hasOwnProperty . call ( obj , key ) ) {
159+ try {
160+ sanitized [ key ] = this . sanitizeForJson ( obj [ key ] , seen ) ;
161+ } catch ( error ) {
162+ // If we can't serialize a property, replace it with error message
163+ sanitized [ key ] = `[Error: ${
164+ error instanceof Error ? error . message : 'Unknown error'
165+ } ]`;
166+ }
167+ }
168+ }
169+ return sanitized ;
170+ }
120171}
0 commit comments