-
Notifications
You must be signed in to change notification settings - Fork 450
feat: add logs:edge-functions command and historical log support #7944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
2c150f2
5cc55d0
ac91779
ccab490
37ed35c
ce57478
5234215
0d7844f
06e3ce5
bd52070
752c16a
a79f5d9
1c399d7
0ba377a
82bd7e5
06bffb4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ | |
| | Subcommand | description | | ||
| |:--------------------------- |:-----| | ||
| | [`logs:deploy`](/commands/logs#logsdeploy) | Stream the logs of deploys currently being built to the console | | ||
| | [`logs:edge-functions`](/commands/logs#logsedge-functions) | Stream netlify edge function logs to the console | | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand why this is plural and In the future, I think we could rename |
||
| | [`logs:function`](/commands/logs#logsfunction) | Stream netlify function logs to the console | | ||
|
|
||
|
|
||
|
|
@@ -33,6 +34,8 @@ | |
| netlify logs:deploy | ||
| netlify logs:function | ||
| netlify logs:function my-function | ||
| netlify logs:edge-functions | ||
| netlify logs:edge-functions --deploy-id <deploy-id> | ||
| ``` | ||
|
|
||
| --- | ||
|
|
@@ -52,6 +55,37 @@ | |
| - `debug` (*boolean*) - Print debugging information | ||
| - `auth` (*string*) - Netlify auth token - can be used to run this command without logging in | ||
|
|
||
| --- | ||
| ## `logs:edge-functions` | ||
|
|
||
| Stream netlify edge function logs to the console | ||
|
|
||
| **Usage** | ||
|
|
||
| ```bash | ||
| netlify logs:edge-functions | ||
| ``` | ||
|
|
||
| **Flags** | ||
|
|
||
| - `deploy-id` (*string*) - Deploy ID to stream edge function logs for | ||
| - `filter` (*string*) - For monorepos, specify the name of the application to run the command in | ||
| - `from` (*string*) - Start date for historical logs (ISO 8601 format) | ||
| - `level` (*string*) - Log levels to stream. Choices are: trace, debug, info, warn, error, fatal | ||
| - `debug` (*boolean*) - Print debugging information | ||
| - `auth` (*string*) - Netlify auth token - can be used to run this command without logging in | ||
| - `to` (*string*) - End date for historical logs (ISO 8601 format, defaults to now) | ||
|
|
||
| **Examples** | ||
|
|
||
| ```bash | ||
| netlify logs:edge-functions | ||
| netlify logs:edge-functions --deploy-id <deploy-id> | ||
| netlify logs:edge-functions --from 2026-01-01T00:00:00Z | ||
| netlify logs:edge-functions --from 2026-01-01T00:00:00Z --to 2026-01-02T00:00:00Z | ||
| netlify logs:edge-functions -l info warn | ||
| ``` | ||
|
|
||
| --- | ||
| ## `logs:function` | ||
|
|
||
|
|
@@ -65,21 +99,27 @@ | |
|
|
||
| **Arguments** | ||
|
|
||
| - functionName - Name of the function to stream logs for | ||
| - functionName - Name or ID of the function to stream logs for | ||
|
Check warning on line 102 in docs/commands/logs.md
|
||
|
|
||
| **Flags** | ||
|
|
||
| - `deploy-id` (*string*) - Deploy ID to look up the function from | ||
|
Check warning on line 106 in docs/commands/logs.md
|
||
| - `filter` (*string*) - For monorepos, specify the name of the application to run the command in | ||
| - `from` (*string*) - Start date for historical logs (ISO 8601 format) | ||
| - `level` (*string*) - Log levels to stream. Choices are: trace, debug, info, warn, error, fatal | ||
| - `debug` (*boolean*) - Print debugging information | ||
| - `auth` (*string*) - Netlify auth token - can be used to run this command without logging in | ||
| - `to` (*string*) - End date for historical logs (ISO 8601 format, defaults to now) | ||
|
|
||
| **Examples** | ||
|
|
||
| ```bash | ||
| netlify logs:function | ||
| netlify logs:function my-function | ||
| netlify logs:function my-function --deploy-id <deploy-id> | ||
| netlify logs:function my-function -l info warn | ||
| netlify logs:function my-function --from 2026-01-01T00:00:00Z | ||
| netlify logs:function my-function --from 2026-01-01T00:00:00Z --to 2026-01-02T00:00:00Z | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { OptionValues } from 'commander' | ||
| import inquirer from 'inquirer' | ||
|
|
||
| import { chalk, log } from '../../utils/command-helpers.js' | ||
| import { getWebSocket } from '../../utils/websockets/index.js' | ||
| import type BaseCommand from '../base-command.js' | ||
|
|
||
| import { | ||
| parseDateToMs, | ||
| buildEdgeFunctionLogsUrl, | ||
| fetchHistoricalLogs, | ||
| printHistoricalLogs, | ||
| formatLogEntry, | ||
| } from './log-api.js' | ||
| import { CLI_LOG_LEVEL_CHOICES_STRING, LOG_LEVELS_LIST } from './log-levels.js' | ||
| import { getName } from './build.js' | ||
|
|
||
| export const logsEdgeFunction = async (options: OptionValues, command: BaseCommand) => { | ||
| let deployId = options.deployId as string | undefined | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also applies to: 32-39 🤖 Prompt for AI Agents |
||
| await command.authenticate() | ||
|
|
||
| const client = command.netlify.api | ||
| const { site } = command.netlify | ||
| const { id: siteId } = site | ||
|
|
||
| if (!siteId) { | ||
| log('You must link a project before attempting to view edge function logs') | ||
| return | ||
| } | ||
|
|
||
| const levels = options.level as string[] | undefined | ||
| if (levels && !levels.every((level) => LOG_LEVELS_LIST.includes(level))) { | ||
| log(`Invalid log level. Choices are:${CLI_LOG_LEVEL_CHOICES_STRING.join(',')}`) | ||
| } | ||
|
|
||
| const levelsToPrint: string[] = levels || LOG_LEVELS_LIST | ||
|
|
||
| if (options.from) { | ||
| const fromMs = parseDateToMs(options.from as string) | ||
| const toMs = options.to ? parseDateToMs(options.to as string) : Date.now() | ||
|
|
||
| const url = buildEdgeFunctionLogsUrl({ siteId, from: fromMs, to: toMs }) | ||
| const data = await fetchHistoricalLogs({ url, accessToken: client.accessToken ?? '' }) | ||
| printHistoricalLogs(data, levelsToPrint) | ||
| return | ||
| } | ||
|
|
||
| const userId = command.netlify.globalConfig.get('userId') as string | ||
|
|
||
| if (!deployId) { | ||
| const deploys = await client.listSiteDeploys({ siteId }) | ||
|
|
||
| if (deploys.length === 0) { | ||
| log('No deploys found for the project') | ||
| return | ||
| } | ||
|
|
||
| if (deploys.length === 1) { | ||
| deployId = deploys[0].id | ||
| } else { | ||
| const { result } = (await inquirer.prompt({ | ||
| name: 'result', | ||
| type: 'list', | ||
| message: `Select a deploy\n\n${chalk.yellow('*')} indicates a deploy created by you`, | ||
| choices: deploys.map((deploy) => ({ | ||
| name: getName({ deploy, userId }), | ||
| value: deploy.id, | ||
| })), | ||
| })) as { result: string } | ||
|
|
||
| deployId = result | ||
| } | ||
| } | ||
|
|
||
| const ws = getWebSocket('wss://socketeer.services.netlify.com/edge-function/logs') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Side note: I hate that we're exposing this. We should set up a customer-facing domain name for this. |
||
|
|
||
| ws.on('open', () => { | ||
| ws.send( | ||
| JSON.stringify({ | ||
| deploy_id: deployId, | ||
| site_id: siteId, | ||
| access_token: client.accessToken, | ||
| since: new Date().toISOString(), | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| ws.on('message', (data: string) => { | ||
| const logData = JSON.parse(data) as { level: string; message: string; timestamp?: string } | ||
| if (!levelsToPrint.includes(logData.level.toLowerCase())) { | ||
| return | ||
| } | ||
| log(formatLogEntry(logData)) | ||
| }) | ||
|
Comment on lines
+88
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard websocket parsing to prevent command crashes. Line 89 assumes every frame is valid JSON; one malformed payload will throw and terminate the stream. 🛡️ Proposed defensive parsing ws.on('message', (data: string) => {
- const logData = JSON.parse(data) as { level: string; message: string; timestamp?: string }
+ let logData: { level: string; message: string; timestamp?: string }
+ try {
+ logData = JSON.parse(data) as { level: string; message: string; timestamp?: string }
+ } catch {
+ log('Received malformed log payload')
+ return
+ }
+
if (!levelsToPrint.includes(logData.level.toLowerCase())) {
return
}
log(formatLogEntry(logData))
})🤖 Prompt for AI Agents |
||
|
|
||
| ws.on('close', () => { | ||
| log('Connection closed') | ||
| }) | ||
|
|
||
| ws.on('error', (err: Error) => { | ||
| log('Connection error') | ||
| log(err.message) | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolve docs lint warnings in the new argument/flag text.
Line 172 (
functionName) and Line 176 (“look up”) will keep triggering lint-docs warnings; rewording here will keep docs clean.✏️ Proposed doc-only fix
Also applies to: 176-176
🧰 Tools
🪛 GitHub Check: lint-docs
[warning] 172-172:
[vale] reported by reviewdog 🐶
[base.spelling] Spellcheck: did you really mean 'functionName'?
Raw Output:
{"message": "[base.spelling] Spellcheck: did you really mean 'functionName'?", "location": {"path": "docs/commands/functions.md", "range": {"start": {"line": 172, "column": 3}}}, "severity": "WARNING"}
🤖 Prompt for AI Agents