UNPKG

@mcma/core

Version:

Node module with type definitions and helper utils for the EBU MCMA framework

76 lines (75 loc) 2.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LogEvent = void 0; const util = require("util"); class LogEvent { type; level; source; requestId; timestamp; message; tracker; constructor(type, level, source, requestId, timestamp, message, tracker) { this.type = type; this.level = level; this.source = source; this.requestId = requestId; this.timestamp = timestamp; this.message = message; this.tracker = tracker; } flatten() { let message = this.message; if (message instanceof Error) { if (this.message.stack) { message = this.message.stack; } else { message = this.message.toString(); } } const logEventEntry = { type: this.type, level: this.level, source: this.source, requestId: this.requestId, timestamp: this.timestamp, message, trackerId: this.tracker?.id, trackerLabel: this.tracker?.label, }; if (this.tracker?.custom) { for (const key of Object.keys(this.tracker?.custom)) { const trackerCustomKey = key.substring(0, 1).toUpperCase() + key.substring(1); const trackerCustomValue = this.tracker?.custom[key]; switch (trackerCustomKey) { case "Id": case "Label": logEventEntry["trackerCustom" + trackerCustomKey] = trackerCustomValue; break; default: logEventEntry["tracker" + trackerCustomKey] = trackerCustomValue; break; } } } return logEventEntry; } toString() { const logEventEntry = this.flatten(); try { return JSON.stringify(logEventEntry, null, 2); } catch { try { logEventEntry.message = util.inspect(logEventEntry.message); } catch { logEventEntry.message = logEventEntry.message + ""; } } return JSON.stringify(logEventEntry, null, 2); } } exports.LogEvent = LogEvent;