fix(logs): map numeric pino levels

Normalize numeric pino levels correctly in the console log API so the logger transport fix does not misclassify info, warn, and error entries in file-backed logs.

Add a targeted regression test for numeric log entries.
This commit is contained in:
Kfir Amar
2026-03-15 01:51:59 +02:00
parent 0fe6e24554
commit 3a3c7a7968
2 changed files with 58 additions and 4 deletions

View File

@@ -26,10 +26,10 @@ const LEVEL_ORDER: Record<string, number> = {
// Map pino numeric levels to string levels
const NUMERIC_LEVEL_MAP: Record<number, string> = {
10: "trace",
20: "info",
30: "warn",
40: "error",
50: "fatal",
20: "debug",
30: "info",
40: "warn",
50: "error",
60: "fatal",
};

View File

@@ -0,0 +1,54 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_LOG_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-console-log-levels-"));
const TEST_LOG_PATH = path.join(TEST_LOG_DIR, "app.log");
const originalLogFilePath = process.env.LOG_FILE_PATH;
process.env.LOG_FILE_PATH = TEST_LOG_PATH;
const route = await import("../../src/app/api/logs/console/route.ts");
test.after(() => {
if (originalLogFilePath === undefined) {
delete process.env.LOG_FILE_PATH;
} else {
process.env.LOG_FILE_PATH = originalLogFilePath;
}
fs.rmSync(TEST_LOG_DIR, { recursive: true, force: true });
});
test("console log API normalizes numeric pino levels correctly", async () => {
fs.writeFileSync(
TEST_LOG_PATH,
[
JSON.stringify({
timestamp: new Date().toISOString(),
level: 30,
module: "probe",
msg: "info entry",
}),
JSON.stringify({
timestamp: new Date().toISOString(),
level: 40,
module: "probe",
msg: "warn entry",
}),
].join("\n") + "\n",
"utf8"
);
const response = await route.GET(
new Request("http://localhost/api/logs/console?level=info&limit=10")
);
const body = await response.json();
assert.equal(response.status, 200);
assert.deepEqual(
body.map((entry) => entry.level),
["info", "warn"]
);
});