Skip to content

Commit

Permalink
Pretty print various objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 20, 2024
1 parent 6343eba commit c5fae30
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 5 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/log",
"version": "0.10.2",
"version": "0.10.3",
"exports": {
".": "./mod.ts",
"./console": "./transports/console.ts",
Expand Down
45 changes: 45 additions & 0 deletions transports/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown, unknown>): 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<unknown>): 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.
Expand Down
5 changes: 4 additions & 1 deletion transports/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 7 additions & 1 deletion transports/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
8 changes: 7 additions & 1 deletion transports/newrelic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
3 changes: 2 additions & 1 deletion transports/splunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down

0 comments on commit c5fae30

Please sign in to comment.