Skip to content
Merged
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
4 changes: 3 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@
"@types/json-schema": "^7.0.15",
"@types/jsonwebtoken": "^9.0.10",
"@types/lodash": "^4.17.24",
"@types/ms": "^2.1.0",
"@types/multer": "^2.0.0",
"@types/node": "^22.10.5",
"@vercel/detect-agent": "^1.1.0",
"outdent": "^0.8.0",
"chalk": "^5.6.2",
"chokidar": "^5.0.0",
"commander": "^12.1.0",
Expand All @@ -70,23 +70,25 @@
"globby": "^16.1.0",
"http-proxy-middleware": "^3.0.5",
"json-schema-to-typescript": "^15.0.4",
"jsonwebtoken": "^9.0.3",
"json5": "^2.2.3",
"jsonwebtoken": "^9.0.3",
"ky": "^1.14.2",
"lodash": "^4.17.23",
"ms": "^2.1.3",
"multer": "^2.0.0",
"nanoid": "^5.1.6",
"open": "^11.0.0",
"outdent": "^0.8.0",
"p-wait-for": "^6.0.0",
"posthog-node": "5.21.2",
"qs": "^6.12.3",
"socket.io": "^4.8.3",
"strip-ansi": "^7.1.2",
"tar": "^7.5.4",
"tmp-promise": "^3.0.3",
"typescript": "^5.7.2",
"vitest": "^4.0.16",
"yaml": "^2.8.2",
"qs": "^6.12.3",
"zod": "^4.3.5"
},
"engines": {
Expand Down
21 changes: 16 additions & 5 deletions packages/cli/src/cli/commands/project/logs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Command } from "commander";
import { Option } from "commander";
import ms, { type StringValue } from "ms";
import type { CLIContext, RunCommandResult } from "@/cli/types.js";
import { Base44Command } from "@/cli/utils/index.js";
import { ApiError, InvalidInputError } from "@/core/errors.js";
Expand Down Expand Up @@ -70,6 +71,10 @@ function parseFunctionNames(option: string | undefined): string[] {
}

function normalizeDatetime(value: string): string {
const duration = ms(value as StringValue);
if (duration !== undefined) {
return new Date(Date.now() - duration).toISOString();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using a library that already solves this?
https://www.npmjs.com/package/ms maybe?

if (/Z$|[+-]\d{2}:\d{2}$/.test(value)) return value;
return `${value}Z`;
}
Expand Down Expand Up @@ -143,8 +148,9 @@ async function fetchLogsForFunctions(
throw error;
}

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

// When fetching multiple functions, merge-sort the combined results
Expand Down Expand Up @@ -216,7 +222,11 @@ async function logsAction(
? `${JSON.stringify(entries, null, 2)}\n`
: formatLogs(entries);

return { outroMessage: "Fetched logs", stdout: logsOutput };
const shouldOutputOutroMessage = !options.json;
return {
outroMessage: shouldOutputOutroMessage ? "Fetched logs" : undefined,
stdout: logsOutput,
};
}

export function getLogsCommand(): Command {
Expand All @@ -228,12 +238,12 @@ export function getLogsCommand(): Command {
)
.option(
"--since <datetime>",
"Show logs from this time (ISO format)",
"Show logs from this time. ISO datetime or relative shorthand (e.g. 1h, 30m, 2d)",
normalizeDatetime,
)
.option(
"--until <datetime>",
"Show logs until this time (ISO format)",
"Show logs until this time. ISO datetime or relative shorthand (e.g. 1h, 30m, 2d)",
normalizeDatetime,
)
.addOption(
Expand All @@ -245,5 +255,6 @@ export function getLogsCommand(): Command {
.addOption(
new Option("--order <order>", "Sort order").choices(["asc", "desc"]),
)
.option("--json", "Output as JSON (clean stdout, safe to pipe to jq)")
.action(logsAction);
}
36 changes: 35 additions & 1 deletion packages/cli/tests/cli/logs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from "vitest";
import { describe, expect, it } from "vitest";
import { fixture, setupCLITests } from "./testkit/index.js";

describe("logs command", () => {
Expand Down Expand Up @@ -287,4 +287,38 @@ describe("logs command", () => {

t.expectResult(result).toSucceed();
});

it("outputs valid JSON with --json", async () => {
await t.givenLoggedInWithProject(fixture("basic"));
t.api.mockFunctionLogs("my-function", [
{ time: "2024-01-15T10:30:00.000Z", level: "info", message: "Hello" },
]);

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

t.expectResult(result).toSucceed();
const stdout = result.stdout ?? "";
const jsonStart = stdout.indexOf("[");
const parsed = JSON.parse(stdout.slice(jsonStart));
expect(parsed).toHaveLength(1);
expect(parsed[0].message).toContain("Hello");
});

it("accepts relative time shortcuts for --since", async () => {
await t.givenLoggedInWithProject(fixture("basic"));
t.api.mockFunctionLogs("my-function", [
{ time: "2024-01-15T10:30:00.000Z", level: "info", message: "Recent" },
]);

const result = await t.run(
"logs",
"--function",
"my-function",
"--since",
"1h",
);

t.expectResult(result).toSucceed();
t.expectResult(result).toContain("Recent");
});
});
Loading