2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertEquals } from "@std/assert";
|
|
|
|
import { FakeTime } from "@std/testing/time";
|
2023-12-18 11:19:47 +00:00
|
|
|
import { jsonFormatter } from "./formatters.ts";
|
|
|
|
import { LogRecord } from "./logger.ts";
|
|
|
|
|
|
|
|
const log = (msg: string, args: unknown[] = []) =>
|
|
|
|
new LogRecord({
|
|
|
|
msg,
|
|
|
|
args,
|
|
|
|
level: 20,
|
|
|
|
loggerName: "user-logger",
|
|
|
|
});
|
|
|
|
|
2024-02-27 20:06:29 +00:00
|
|
|
Deno.test("jsonFormatter() handles messages without arguments", function () {
|
2023-12-18 11:19:47 +00:00
|
|
|
using _time = new FakeTime(1);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
jsonFormatter(log("msg")),
|
|
|
|
`{"level":"INFO","datetime":1,"message":"msg"}`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-02-27 20:06:29 +00:00
|
|
|
Deno.test("jsonFormatter() handles messages with one arguments", function () {
|
2023-12-18 11:19:47 +00:00
|
|
|
using _time = new FakeTime(1);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
jsonFormatter(log("msg", [{ user: "Dave" }])),
|
|
|
|
`{"level":"INFO","datetime":1,"message":"msg","args":{"user":"Dave"}}`,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-02-27 20:06:29 +00:00
|
|
|
Deno.test("jsonFormatter() handles messages with many arguments", function () {
|
2023-12-18 11:19:47 +00:00
|
|
|
using _time = new FakeTime(1);
|
|
|
|
|
|
|
|
assertEquals(
|
|
|
|
jsonFormatter(log("msg", [1, true, null, [], {}])),
|
|
|
|
`{"level":"INFO","datetime":1,"message":"msg","args":[1,true,null,[],{}]}`,
|
|
|
|
);
|
|
|
|
});
|