Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dist/services/LoggerService.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,12 @@ export default class LoggerService {
};
refineMessagePerTransport(transportName: string, message: any): any;
getTransportByName(transportName: string): Transport | undefined;
/**
* Sanitizes an object for JSON serialization by:
* - Replacing functions with a string representation (similar to console.log behavior)
* - Handling circular references
* - Preserving other values
*/
sanitizeForJson(obj: any, seen?: WeakSet<object>): any;
}
export {};
53 changes: 52 additions & 1 deletion dist/services/LoggerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export default class LoggerService {
if (!this.checkIsLogRequired('console', level)) return null;
const refinedMessage = this.refineMessagePerTransport('console', message);
const consoleFunc = level === 'notice' ? 'log' : level;
return console[consoleFunc](JSON.stringify(refinedMessage));
const sanitizedMessage = this.sanitizeForJson(refinedMessage);
return console[consoleFunc](JSON.stringify(sanitizedMessage));
}
checkIsLogRequired(transportName, level) {
const transport = this.getTransportByName(transportName);
Expand Down Expand Up @@ -117,4 +118,54 @@ export default class LoggerService {
transport => transport.logType === transportName
);
}
/**
* Sanitizes an object for JSON serialization by:
* - Replacing functions with a string representation (similar to console.log behavior)
* - Handling circular references
* - Preserving other values
*/
sanitizeForJson(obj, seen = new WeakSet()) {
// Handle null and undefined
if (obj === null || obj === undefined) {
return obj;
}
// Handle functions - replace with string representation like console.log does
if (typeof obj === 'function') {
const funcName = obj.name || 'anonymous';
return `[Function: ${funcName}]`;
}
// Handle primitives
if (typeof obj !== 'object') {
return obj;
}
// Handle circular references
if (seen.has(obj)) {
return '[Circular]';
}
// Handle Date objects
if (obj instanceof Date) {
return obj.toISOString();
}
// Handle arrays
if (Array.isArray(obj)) {
seen.add(obj);
return obj.map(item => this.sanitizeForJson(item, seen));
}
// Handle objects
seen.add(obj);
const sanitized = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
try {
sanitized[key] = this.sanitizeForJson(obj[key], seen);
} catch (error) {
// If we can't serialize a property, replace it with error message
sanitized[key] = `[Error: ${
error instanceof Error ? error.message : 'Unknown error'
}]`;
}
}
}
return sanitized;
}
}
62 changes: 61 additions & 1 deletion src/services/LoggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ export default class LoggerService {
if (!this.checkIsLogRequired('console', level)) return null;
const refinedMessage = this.refineMessagePerTransport('console', message);
const consoleFunc = level === 'notice' ? 'log' : level;
const sanitizedMessage = this.sanitizeForJson(refinedMessage);

return console[consoleFunc](JSON.stringify(refinedMessage));
return console[consoleFunc](JSON.stringify(sanitizedMessage));
}

checkIsLogRequired(transportName: string, level: LogLevel) {
Expand Down Expand Up @@ -187,4 +188,63 @@ export default class LoggerService {
transport => transport.logType === transportName
);
}

/**
* Sanitizes an object for JSON serialization by:
* - Replacing functions with a string representation (similar to console.log behavior)
* - Handling circular references
* - Preserving other values
*/
sanitizeForJson(obj: any, seen = new WeakSet()): any {
// Handle null and undefined
if (obj === null || obj === undefined) {
return obj;
}

// Handle functions - replace with string representation like console.log does
if (typeof obj === 'function') {
const funcName = obj.name || 'anonymous';
return `[Function: ${funcName}]`;
}

// Handle primitives
if (typeof obj !== 'object') {
return obj;
}

// Handle circular references
if (seen.has(obj)) {
return '[Circular]';
}

// Handle Date objects
if (obj instanceof Date) {
return obj.toISOString();
}

// Handle arrays
if (Array.isArray(obj)) {
seen.add(obj);
return obj.map(item => this.sanitizeForJson(item, seen));
}

// Handle objects
seen.add(obj);
const sanitized: any = {};

for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
try {
sanitized[key] = this.sanitizeForJson(obj[key], seen);
} catch (error) {
// If we can't serialize a property, replace it with error message
sanitized[key] = `[Error: ${
error instanceof Error ? error.message : 'Unknown error'
}]`;
}
}
}

return sanitized;
}
}
Loading