1. /**
  2. * Strips metadata from log entries.
  3. *
  4. * @param {any} logEntry The log entry to process.
  5. * @returns {any} The log entry with metadata stripped, or the original entry if an error occurs.
  6. */
  7. function stripMetadata(logEntry) {
  8. try {
  9. // Check if the log entry is an object.
  10. if (typeof logEntry === 'object' && logEntry !== null) {
  11. // Create a copy to avoid modifying the original object.
  12. const strippedEntry = { ...logEntry };
  13. // Remove common metadata fields. Add more as needed.
  14. delete strippedEntry.timestamp;
  15. delete strippedEntry.source;
  16. delete strippedEntry.level;
  17. delete strippedEntry.hostname;
  18. delete strippedEntry.pid;
  19. delete strippedEntry.tid;
  20. delete strippedEntry.processName;
  21. delete strippedEntry.user;
  22. delete strippedEntry.address;
  23. delete strippedEntry.functionName;
  24. delete strippedEntry.line;
  25. return strippedEntry;
  26. } else {
  27. // If not an object, return the entry as is.
  28. return logEntry;
  29. }
  30. } catch (error) {
  31. // Handle errors gracefully. Log the error for debugging.
  32. console.error("Error stripping metadata:", error);
  33. return logEntry; // Return original on failure.
  34. }
  35. }
  36. // Example Usage (for testing)
  37. // const logEntry = {
  38. // timestamp: Date.now(),
  39. // source: "my_app",
  40. // level: "INFO",
  41. // message: "This is a log message",
  42. // hostname: "localhost",
  43. // pid: 1234,
  44. // functionName: "myFunction",
  45. // line: 10,
  46. // };
  47. //
  48. // const strippedLogEntry = stripMetadata(logEntry);
  49. // console.log(strippedLogEntry);

Add your comment