0Pricing
Supabase Backend as a Service · 课时

监控、日志记录与可观测性

通过读取日志、跟踪关键指标、为 Edge Functions 添加监测,以及在小问题演变成服务中断前设置告警,保持生产环境中的 Supabase 后端健康运行。

监控、日志记录与可观测性 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Observability Matters

Deploying is only half the job. Once your backend serves real traffic you need to see what it is doing. Observability is the practice of understanding system state from its outputs.

  • Logs tell you what happened
  • Metrics tell you how much and how fast
  • Traces tell you where time was spent

The Supabase Logs Explorer

Supabase exposes logs for every service in the dashboard under Logs: API gateway, Postgres, Auth, Storage, and Edge Functions. Each is queryable with SQL-like syntax.

Filtering by status code and path quickly surfaces failing requests.

Querying Logs

The Logs Explorer accepts SQL over structured log events. Here we count API errors grouped by path over the last hour.

select path, count(*) as errors
from edge_logs
where status_code >= 500
group by path
order by errors desc;

Structured Logging in Edge Functions

Inside Edge Functions, prefer structured JSON logs over plain strings. They are far easier to filter and aggregate later.

console.log(JSON.stringify({
  level: 'info',
  event: 'order_created',
  orderId: order.id,
  userId: user.id,
  durationMs: Date.now() - start
}));

Log Levels

Use consistent levels so you can filter noise from signal.

  • debug for development detail
  • info for normal events
  • warn for recoverable problems
  • error for failures needing attention

In production, avoid logging at debug to control cost and volume.

Key Database Metrics

Watch a handful of Postgres metrics closely:

  • Active connections vs the pool limit
  • Cache hit ratio (aim above 99%)
  • Slow query count
  • Disk and CPU usage

Supabase surfaces these on the Reports page.

Finding Slow Queries

Enable pg_stat_statements to see which queries consume the most total time. This is the single most useful tool for performance triage.

select query, calls, mean_exec_time, total_exec_time
from pg_stat_statements
order by total_exec_time desc
limit 10;

Never Log Secrets

Logs are persisted and often shared. Never write passwords, tokens, full card numbers, or service-role keys into logs.

  • Redact sensitive fields before logging
  • Log identifiers, not raw payloads
  • Treat log access as a security boundary

Correlating Requests

Attach a request ID to every log line in a function so you can reconstruct a single request's full journey across logs.

const requestId = crypto.randomUUID();
console.log(JSON.stringify({ requestId, event: 'start' }));
// ... work ...
console.log(JSON.stringify({ requestId, event: 'done' }));

Alerting

Logs you never look at help no one. Set up alerts for the conditions that matter: error-rate spikes, connection exhaustion, disk nearing capacity.

Forward logs to an external sink (e.g. via webhooks or a log drain) when you need richer alerting than the dashboard provides.

Retention and Cost

Logs cost money and storage. Decide a retention policy deliberately.

  • Keep high-value audit logs longer
  • Sample or drop chatty debug logs
  • Archive to cheap storage before deletion if needed for compliance

Quick Check

Test your observability knowledge.

Recap

You now have a foundation for production observability.

  • Use the Logs Explorer and structured JSON logs
  • Apply consistent log levels and request IDs
  • Track connections, cache hit ratio, and slow queries
  • Use pg_stat_statements for performance triage
  • Never log secrets; set alerts and a retention policy

常见问题解答

「监控、日志记录与可观测性」课时是免费的吗?

是的 — 「监控、日志记录与可观测性」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 4 节课。

「监控、日志记录与可观测性」这节课中我会学到什么?

通过读取日志、跟踪关键指标、为 Edge Functions 添加监测,以及在小问题演变成服务中断前设置告警,保持生产环境中的 Supabase 后端健康运行。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Supabase Backend as a Service 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「监控、日志记录与可观测性」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?

能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 环境管理与持续集成/持续交付
  2. 备份、恢复与灾难恢复
  3. 安全审计与最佳实践
  4. 监控、日志记录与可观测性
← 返回 Supabase Backend as a Service