mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-19 21:52:21 +03:00
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:
@@ -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",
|
||||
};
|
||||
|
||||
|
||||
54
tests/unit/console-log-levels.test.mjs
Normal file
54
tests/unit/console-log-levels.test.mjs
Normal 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"]
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user