From c5fae30bf5e792a91bc3217b4b47194cb02868c3 Mon Sep 17 00:00:00 2001 From: Hexagon Date: Wed, 20 Mar 2024 01:46:13 +0100 Subject: [PATCH] Pretty print various objects --- deno.json | 2 +- transports/base.ts | 45 ++++++++++++++++++++++++++++++++++++++++++ transports/console.ts | 5 ++++- transports/file.ts | 8 +++++++- transports/newrelic.ts | 8 +++++++- transports/splunk.ts | 3 ++- 6 files changed, 66 insertions(+), 5 deletions(-) diff --git a/deno.json b/deno.json index ee09c5d..6d2dcf4 100644 --- a/deno.json +++ b/deno.json @@ -1,6 +1,6 @@ { "name": "@cross/log", - "version": "0.10.2", + "version": "0.10.3", "exports": { ".": "./mod.ts", "./console": "./transports/console.ts", diff --git a/transports/base.ts b/transports/base.ts index 60992cf..8142e11 100644 --- a/transports/base.ts +++ b/transports/base.ts @@ -55,6 +55,51 @@ export abstract class LogTransportBase implements LogTransport { timestamp: Date, ): void; + /** + * Serializes an unknown value into a text representation. + * + * @param value - The value to serialize. + * @returns A text representation of the value. + */ + protected serializeToText(data: unknown[]): string[] { + return data.map((item) => { + if (item instanceof Map) { + return this.serializeMap(item); + } else if (item instanceof Set) { + return this.serializeSet(item); + } else if (typeof item === "object") { + return JSON.stringify(item); // Pretty-print other objects + } else { + return item !== undefined ? item.toString() : "undefined"; + } + }); + } + /** + * Serializes a Map into a readable text representation. + * + * @param map - The Map to serialize. + * @returns A text representation of the Map. + */ + private serializeMap(map: Map): string { + const entries = Array.from(map.entries()).map(([key, value]) => + `${this.serializeToText([key])} => ${this.serializeToText([value])}` + ); + return `Map:{ ${entries.join(", ")} }`; + } + + /** + * Serializes a Set into a readable text representation. + * + * @param set - The Set to serialize. + * @returns A text representation of the Set. + */ + private serializeSet(set: Set): string { + const items = Array.from(set.values()).map((v) => + this.serializeToText([v]) + ); + return `Set:{ ${items.join(", ")} }`; + } + /** * Determines if the message should be logged based on its severity and the configured log level. * @param level - The severity level of the message. diff --git a/transports/console.ts b/transports/console.ts index 20f5623..1850213 100644 --- a/transports/console.ts +++ b/transports/console.ts @@ -41,7 +41,10 @@ export class ConsoleLogger extends LogTransportBase { let styledLevel = level.toString().padEnd(5, " "); - let message = `${scope}: ${data.join(" ")}`; + // Serialize objects in the data array + const serializedData = this.serializeToText(data); + + let message = `${scope}: ${serializedData.join(" ")}`; switch (level) { case Severity.Debug: diff --git a/transports/file.ts b/transports/file.ts index f959182..83ebdff 100644 --- a/transports/file.ts +++ b/transports/file.ts @@ -51,7 +51,13 @@ export class FileLogger extends LogTransportBase implements LogTransport { */ log(level: Severity, scope: string, data: unknown[], timestamp: Date) { if (this.shouldLog(level)) { - const message = this.formatMessage(level, scope, data, timestamp); + const serializedData = this.serializeToText(data); + const message = this.formatMessage( + level, + scope, + serializedData, + timestamp, + ); appendFile(this.options.filePath!, message) .catch((err) => console.error(`Error writing to log file:`, err)); } diff --git a/transports/newrelic.ts b/transports/newrelic.ts index 01c5e6a..1962a1b 100644 --- a/transports/newrelic.ts +++ b/transports/newrelic.ts @@ -27,7 +27,13 @@ export class NewRelicLogger extends LogTransportBase implements LogTransport { log(severity: Severity, scope: string, data: unknown[], timestamp: Date) { if (this.shouldLog(severity)) { - const event = this.formatEvent(severity, scope, data, timestamp); + const serializedData = this.serializeToText(data); + const event = this.formatEvent( + severity, + scope, + serializedData, + timestamp, + ); this.sendToNewRelic(event); } } diff --git a/transports/splunk.ts b/transports/splunk.ts index f59ab3c..17f5c06 100644 --- a/transports/splunk.ts +++ b/transports/splunk.ts @@ -28,7 +28,8 @@ export class SplunkHecLogger extends LogTransportBase implements LogTransport { } log(level: Severity, scope: string, data: unknown[], timestamp: Date) { if (this.shouldLog(level)) { - const event = this.formatEvent(level, scope, data, timestamp); + const serializedData = this.serializeToText(data); + const event = this.formatEvent(level, scope, serializedData, timestamp); this.sendToHec(event); } }