Skip to content

Commit 6f77bfe

Browse files
Daison Carinomosufy
authored andcommitted
feat: implement JSON sanitization in LoggerService
- Added `sanitizeForJson` method to handle functions, circular references, and other data types for safe JSON serialization. - Updated logging methods to utilize the new sanitization, ensuring consistent and safe output for complex objects.
1 parent b191e9d commit 6f77bfe

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

dist/services/LoggerService.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,12 @@ export default class LoggerService {
4242
};
4343
refineMessagePerTransport(transportName: string, message: any): any;
4444
getTransportByName(transportName: string): Transport | undefined;
45+
/**
46+
* Sanitizes an object for JSON serialization by:
47+
* - Replacing functions with a string representation (similar to console.log behavior)
48+
* - Handling circular references
49+
* - Preserving other values
50+
*/
51+
sanitizeForJson(obj: any, seen?: WeakSet<object>): any;
4552
}
4653
export {};

dist/services/LoggerService.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)