Debug Realtime with Logger and Log Levels
logger 和 log_level 参数是什么? #
🌐 What are logger and log_level parameters?
实时客户端提供内置日志功能,帮助你调试连接问题、跟踪消息,并了解实时连接的情况。有两个参数可以控制日志记录:
🌐 The Realtime client provides built-in logging to help you debug connection issues, track messages, and understand what's happening with your real-time connections. Two parameters control logging:
logger:一个处理日志消息的自定义函数logLevel: 控制服务器记录哪些消息(info、warn或error)
默认情况下,日志记录是禁用的。启用后,你可以看到:
🌐 By default, logging is disabled. When enabled, you can see:
- 发送和接收的消息
- 连接事件(连接、断开、错误)
- 心跳状态
- 通道订阅
- 员工活动
如何在你的实时客户端启用日志记录 #
🌐 How to enable logging in your Realtime client
基本控制台日志 #
🌐 Basic logging to console
import { createClient } from '@supabase/supabase-js'const supabase = createClient(SUPABASE_URL, SUPABASE_KEY, { realtime: { logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }, },})带有服务器端过滤的日志级别 #
🌐 With log levels for server-side filtering
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY, { realtime: { logLevel: 'info', // 'info' | 'warn' | 'error' logger: (kind, msg, data) => { console.log(`[${kind}] ${msg}`, data) }, },})理解日志信息 #
🌐 Understanding log messages
记录器接收三个参数:kind、msg 和 data。
🌐 The logger receives three parameters: kind, msg, and data.
你会看到的日志类型:
push - 发送到服务器的消息
push: realtime:chat heartbeat (42) {}push: realtime:chat phx_join (1) { config: {...} }receive - 从服务器接收到的消息
receive: ok realtime:chat phx_reply (1) { response: {...} }receive: realtime:chat broadcast { event: 'message', payload: {...} }transport - 连接事件
transport: connected to wss://project.supabase.co/realtime/v1transport: heartbeat timeout. Attempting to re-establish connectiontransport: close CloseEvent {...}transport: leaving duplicate topic "realtime:chat"error - 错误事件
error: error in heartbeat callback Error: ...error: error waiting for auth on connect Error: ...worker - Web Worker 事件
worker: starting default workerworker: starting worker for from /worker.jsworker: worker error message常见调试场景
调试通道无法订阅的原因:
import { createClient } from '@supabase/supabase-js'const supabase = createClient(SUPABASE_URL, SUPABASE_KEY, { realtime: { logger: (kind, msg, data) => { console.log(`[${kind}] ${msg}`, data) }, },})const channel = supabase .channel('debug-channel') .on('broadcast', { event: 'test' }, (payload) => { console.log('Received:', payload) }) .subscribe((status, err) => { console.log('Subscribe status:', status, err) })// Check logs for:// push: realtime:debug-channel phx_join - subscription attempt// receive: ok realtime:debug-channel phx_reply - successful subscription// Or error messages if subscription failed调试消息投递问题 #
🌐 Debug message delivery issues
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY, { realtime: { logger: (kind, msg, data) => { if (kind === 'push' || kind === 'receive') { console.log(`[${kind}] ${msg}`, data) } }, },})const channel = supabase.channel('chat', { config: { broadcast: { self: true } } })channel.on('broadcast', { event: 'message' }, (payload) => { console.log('Message received:', payload)})// Send message after subscribingchannel.subscribe((status, err) => { if (status == 'SUBSCRIBED') { channel.send({ type: 'broadcast', event: 'message', payload: { text: 'Hello' }, }) } else { console.error({ status, err }) }})// Check logs:// push: realtime:chat broadcast - message sent// receive: realtime:chat broadcast - message received (if self: true)