在 Web PubSub for Socket.IO 中收集日志

与自承载 Socket.IO 库时一样,使用 Web PubSub for Socket.IO 时,可以在服务器和客户端收集日志。

服务器端

服务器端包含两个提供调试功能的实用工具:

  • DEBUG,Web PubSub 提供的 Socket.IO 库和扩展库,用于某些日志记录。
  • @azure/logger,提供较低级别的网络相关日志记录。 此外,它还让你可以方便地设置日志级别。

DEBUG JavaScript 实用工具

记录所有调试信息

DEBUG=* node yourfile.js

记录特定包中的调试信息

# Logs debug information from the "socket.io" package
DEBUG=socket.io:* node yourfile.js

# Logs debug information from the "engine.io" package
DEBUG=engine:* node yourfile.js

# Logs debug information from the extension library "wps-sio-ext" provided by Web PubSub
DEBUG=wps-sio-ext:* node yourfile.js

# Logs debug information from multiple packages
DEBUG=engine:*,socket.io:*,wps-sio-ext:* node yourfile.js

Screenshot of logging information from DEBUG JavaScript utility

@azure/logger 实用工具

可以通过设置环境变量 AZURE_LOG_LEVEL 来启用 @azure/logger 实用工具中的日志记录以获取较低级别的网络相关信息。

AZURE_LOG_LEVEL=verbose node yourfile.js

Azure_LOG_LEVEL 有四个级别:verboseinfowarningerror

Screenshot of logging information from the logger utility.

客户端

使用 Web PubSub for Socket.IO 不会改变你调试 Socket.IO 库的方式。 参阅 Socket.IO 库的文档

在 Node 中调试 Socket.IO 客户端

# Logs all debug information
DEBUG=* node yourfile.js

# Logs debug information from the "socket.io-client" package 
DEBUG=socket.io-client:* node yourfile.js

# Logs debug information from the "engine.io-client" package 
DEBUG=engine.io-client:* node yourfile.js

# Logs debug information from multiple packages 
DEBUG=socket.io-client:*,engine.io-client* node yourfile.js

在浏览器中调试 Socket.IO 客户端

在浏览器中,使用 localStorage.debug = '<scope>'

# Logs all debug information
localStorage.debug = '*';

# Logs debug information from the "socket.io-client" package 
localStorage.debug = 'socket.io-client';