Skip to content
Edge Functions

伐木

Monitor your Edge Functions with logging to track execution, debug issues, and optimize performance.

每次函数调用都会提供日志,无论是在本地还是在托管环境中。

🌐 Logs are provided for each function invocation, locally and in hosted environments.


正在访问日志 #

🌐 Accessing logs

生产 #

🌐 Production

从仪表板的功能部分访问日志:

🌐 Access logs from the Functions section of your Dashboard:

  1. 导航到仪表板的功能部分
  2. 从列表中选择你的功能
  3. 选择你的日志视图:
    • 调用信息: 请求/响应数据,包括头信息、正文、状态码和执行时长。可以按日期、时间或状态码筛选。
    • 日志: 平台事件、未捕获的异常和自定义日志消息。可以按时间戳、级别或消息内容过滤。

Function invocations.

发展 #

🌐 Development

当你在本地开发时,你会在本地终端窗口看到错误信息和控制台日志。

🌐 When developing locally you will see error messages and console log statements printed to your local terminal window.


日志事件类型 #

🌐 Log event types

自动日志 #

🌐 Automatic logs

你的函数会自动捕获几种类型的事件:

🌐 Your functions automatically capture several types of events:

  • 未捕获的异常:函数在执行过程中抛出的未捕获异常会自动记录。你可以在日志工具中查看错误信息和堆栈跟踪。
  • 自定义日志事件:你可以在代码中使用 console.log、console.error 和 console.warn 来发出自定义日志事件。这些事件也会出现在日志工具中。
  • 启动和关机日志:日志工具的覆盖范围扩展到包括函数的启动和关机日志。

自定义日志 #

🌐 Custom logs

你可以用标准控制台方法添加自己的日志信息:

🌐 You can add your own log messages using standard console methods:

Deno.serve(async (req) => {
try {
const { name } = await req.json()
if (!name) {
// Log a warning message
console.warn('Empty name parameter received')
}
// Log a message
console.log(`Processing request for: ${name}`)
const data = {
message: `Hello ${name || 'Guest'}!`,
}
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' },
})
} catch (error) {
// Log an error message
console.error(`Request processing failed: ${error.message}`)
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
status: 500,
headers: { 'Content-Type': 'application/json' },
})
}
})

记录小贴士 #

🌐 Logging tips

记录请求头 #

🌐 Logging request headers

在调试 Edge Functions 时,一个常见错误是尝试通过这样的代码把 headers 打到开发者控制台:

🌐 When debugging Edge Functions, a common mistake is to try to log headers to the developer console via code like this:

// ❌ This doesn't work as expected
Deno.serve(async (req) => {
console.log(`Headers: ${JSON.stringify(req.headers)}`) // Outputs: "{}"
})

req.headers 对象看起来是空的,因为 Headers 对象不会将数据存储在可枚举的 JavaScript 属性中,这使得它们对 JSON.stringify() 来说是不可见的。

🌐 The req.headers object appears empty because Headers objects don't store data in enumerable JavaScript properties, making them opaque to JSON.stringify().

相反,你得先把 headers 转成普通对象,比如用 Object.fromEntries。

🌐 Instead, you have to convert headers to a plain object first, for example using Object.fromEntries.

// ✅ This works correctly
Deno.serve(async (req) => {
const headersObject = Object.fromEntries(req.headers)
const headersJson = JSON.stringify(headersObject, null, 2)
console.log(`Request headers:\n${headersJson}`)
})

这就会得到类似这样的结果:

🌐 This results in something like:

Request headers: {
"accept": "*/*",
"accept-encoding": "gzip",
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InN1cGFuYWNobyIsInJvbGUiOiJhbm9uIiwieW91IjoidmVyeSBzbmVha3ksIGh1aD8iLCJpYXQiOjE2NTQ1NDA5MTYsImV4cCI6MTk3MDExNjkxNn0.cwBbk2tq-fUcKF1S0jVKkOAG2FIQSID7Jjvff5Do99Y",
"cdn-loop": "cloudflare; subreqs=1",
"cf-ew-via": "15",
"cf-ray": "8597a2fcc558a5d7-GRU",
"cf-visitor": "{\"scheme\":\"https\"}",
"cf-worker": "supabase.co",
"content-length": "20",
"content-type": "application/x-www-form-urlencoded",
"host": "edge-runtime.supabase.com",
"my-custom-header": "abcd",
"user-agent": "curl/8.4.0",
"x-deno-subhost": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImtpZCI6InN1cGFiYXNlIn0.eyJkZXBsb3ltZW50X2lkIjoic3VwYW5hY2hvX2M1ZGQxMWFiLTFjYmUtNDA3NS1iNDAxLTY3ZTRlZGYxMjVjNV8wMDciLCJycGNfcm9vdCI6Imh0dHBzOi8vc3VwYWJhc2Utb3JpZ2luLmRlbm8uZGV2L3YwLyIsImV4cCI6MTcwODYxMDA4MiwiaWF0IjoxNzA4NjA5MTgyfQ.-fPid2kEeEM42QHxWeMxxv2lJHZRSkPL-EhSH0r_iV4",
"x-forwarded-host": "edge-runtime.supabase.com",
"x-forwarded-port": "443",
"x-forwarded-proto": "https"
}