Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/cli/src/cli/commands/project/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,15 @@ async function fetchLogsForFunctions(
throw error;
}

// The backend does not filter by level for every runtime (per-app
// Cloudflare deployments return the full stream), so filter defensively
// here. Entry levels are already normalized by the response schema.
const matchingLogs = filters.level
? logs.filter((entry) => entry.level === filters.level)
: logs;

allEntries.push(
...logs.map((entry) => normalizeLogEntry(entry, functionName)),
...matchingLogs.map((entry) => normalizeLogEntry(entry, functionName)),
);
}

Expand Down
65 changes: 65 additions & 0 deletions packages/cli/tests/cli/logs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,71 @@ describe("logs command", () => {
t.expectResult(result).toContain("Error message");
});

it("drops other levels client-side when the backend ignores --level", async () => {
await t.givenLoggedInWithProject(fixture("basic"));
// The mock, like per-app Cloudflare deployments, ignores the level query
// param and returns the full stream.
t.api.mockFunctionLogs("my-function", [
{
time: "2024-01-15T10:30:00.050Z",
level: "error",
message: "Error message",
},
{
time: "2024-01-15T10:30:01.050Z",
level: "info",
message: "Info message",
},
{
time: "2024-01-15T10:30:02.050Z",
// The wire value predates schema normalization to "warning".
level: "warn" as "warning",
message: "Warn message normalized to warning",
},
]);

const result = await t.run(
"logs",
"--function",
"my-function",
"--level",
"error",
);

t.expectResult(result).toSucceed();
t.expectResult(result).toContain("Error message");
t.expectResult(result).toNotContain("Info message");
t.expectResult(result).toNotContain("Warn message");
});

it("keeps normalized warn entries when filtering by --level warning", async () => {
await t.givenLoggedInWithProject(fixture("basic"));
t.api.mockFunctionLogs("my-function", [
{
time: "2024-01-15T10:30:00.050Z",
level: "warn" as "warning",
message: "Warn message",
},
{
time: "2024-01-15T10:30:01.050Z",
level: "info",
message: "Info message",
},
]);

const result = await t.run(
"logs",
"--function",
"my-function",
"--level",
"warning",
);

t.expectResult(result).toSucceed();
t.expectResult(result).toContain("Warn message");
t.expectResult(result).toNotContain("Info message");
});

it("fails with invalid level option", async () => {
await t.givenLoggedInWithProject(fixture("basic"));

Expand Down
Loading