function serializeLogStream(logStream) {
try {
// Attempt to serialize using JSON.stringify
return JSON.stringify(logStream, null, 2); // Pretty print with 2 spaces
} catch (error) {
// Fallback: Convert to a string representation if JSON serialization fails
console.error("JSON serialization failed. Using string fallback.", error);
return String(logStream);
}
}
// Example Usage (for testing)
const logData = {
timestamp: Date.now(),
level: "info",
message: "This is a test log message.",
data: {
user_id: 123,
event: "page_view"
}
};
const serializedString = serializeLogStream(logData);
console.log(serializedString);
//Example with non-serializable object
const logData2 = {
timestamp: Date.now(),
level: "info",
message: "This is a test log message.",
functionName: function(){console.log("hello")}
}
const serializedString2 = serializeLogStream(logData2);
console.log(serializedString2);
Add your comment