-
Notifications
You must be signed in to change notification settings - Fork 988
/
Copy pathfunctions-log.ts
45 lines (41 loc) · 1.62 KB
/
functions-log.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import * as opn from "open";
import { URLSearchParams } from "url";
import { Command } from "../command";
import { FirebaseError } from "../error";
import { logger } from "../logger";
import * as cloudlogging from "../gcp/cloudlogging";
import * as functionsLog from "../functions/functionslog";
import { needProjectId } from "../projectUtils";
import { requirePermissions } from "../requirePermissions";
export const command = new Command("functions:log")
.description("read logs from deployed functions")
.option(
"--only <function_names>",
'only show logs of specified, comma-seperated functions (e.g. "funcA,funcB")',
)
.option("-n, --lines <num_lines>", "specify number of log lines to fetch")
.option("--open", "open logs page in web browser")
.before(requirePermissions, ["logging.logEntries.list", "logging.logs.list"])
.action(async (options: any) => {
try {
const projectId = needProjectId(options);
const apiFilter = functionsLog.getApiFilter(options.only);
const filterParams = new URLSearchParams(apiFilter);
const url = `https://console.developers.google.com/logs/viewer?advancedFilter=${filterParams.toString()}&project=${projectId}`;
if (options.open) {
opn(url);
return;
}
const entries = await cloudlogging.listEntries(
projectId,
apiFilter,
options.lines || 35,
"desc",
);
functionsLog.logEntries(entries);
logger.info(`\nSee full logs at: ${url}`);
return entries;
} catch (err: any) {
throw new FirebaseError(`Failed to list log entries ${err.message}`, { exit: 1 });
}
});