Skip to content

Commit

Permalink
fix(log): output objects and json correctly to console
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeidx committed Sep 26, 2022
1 parent b4834d4 commit 10cb931
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
22 changes: 12 additions & 10 deletions log/handlers/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,32 @@ export class ConsoleHandler extends LogHandler {

override handle(
{ loggerName, record }: LogHandlerOptions,
): string {
// deno-lint-ignore no-explicit-any
): any {
const formatted = this.format({ loggerName, record });

if (getLevelValue(record.level) <= getLevelValue(LogLevel.ERROR)) {
console.error(formatted);
console.error(...formatted);
} else if (record.level === LogLevel.WARNING) {
console.warn(formatted);
console.warn(...formatted);
} else if (record.level === LogLevel.DEBUG) {
console.debug(formatted);
console.debug(...formatted);
} else {
console.log(formatted);
console.log(...formatted);
}

return formatted;
}

override format({ loggerName, record }: LogHandlerOptions): string {
// deno-lint-ignore no-explicit-any
override format({ loggerName, record }: LogHandlerOptions): any {
if (this.#json) {
return JSON.stringify({
return [JSON.stringify({
...(this.#name && { name: loggerName }),
...(this.#timestamp && { timestamp: record.timestamp }),
level: record.level,
message: record.message,
});
})];
}

const output = [];
Expand All @@ -78,9 +80,9 @@ export class ConsoleHandler extends LogHandler {

const level = this.#formatLevel(record.level);

output.push(level, record.message);
output.push(level, ...record.message);

return output.join(" ");
return output;
}

#formatLevel(level: LogLevel): string {
Expand Down
2 changes: 1 addition & 1 deletion log/handlers/console_test.fixture.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ const logger = new Logger("json", {
],
});

logger.debug("hello", "there");
logger.debug("hello", { key: "value" }, [1, 2, 3], null, true);
12 changes: 12 additions & 0 deletions log/handlers/console_test.fixture.object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Logger } from "../logger.ts";
import { ConsoleHandler } from "./console.ts";

const logger = new Logger("object", {
handlers: [
new ConsoleHandler({
color: false,
}),
],
});

logger.debug({ key: "value" });
7 changes: 6 additions & 1 deletion log/handlers/console_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ Deno.test("ConsoleHandler prints the timestamp for a log record", async () => {
);
});

Deno.test("ConsoleHandler prints stringified objects", async () => {
const [stdout] = await runFixture("object");
assertEquals(getLine(stdout), `debug { key: "value" }`);
});

Deno.test("ConsoleHandler formats a record to JSON", async () => {
const [stdout] = await runFixture("json");
assertEquals(
getLine(stdout),
`{"level":"debug","message":["hello","there"]}`,
`{"level":"debug","message":["hello",{"key":"value"},[1,2,3],null,true]}`,
);
});

Expand Down

0 comments on commit 10cb931

Please sign in to comment.