调试

调试您的Redis Stack函数的方法

概述

有两种方法可以用来调试你的 Redis Stack 函数:

  1. 明智地使用redis.log函数,该函数将内容写入Redis日志文件。
  2. 使用 Redis pub/sub

使用 redis.log

如果您可以访问Redis日志文件,redis.log是调试时使用的好方法。然而,有一个缺点。Redis Cloud用户无法访问Redis日志文件,而且在自托管安装中,通常只有系统管理员才能访问它们。幸运的是,您也可以使用Redis发布/订阅功能,这将在下一节中讨论。

你不需要做任何特别的事情来使用redis.log,因为它总是可用的。这里有一个例子:

#!js api_version=1.0 name=lib

redis.registerFunction('hello', ()=> {
  redis.log('Hello log')
  return 'Hello from an external file'
})

加载库并使用TFCALL执行函数后,您将在Redis日志文件中看到类似以下内容:

45718:M 01 Nov 2023 07:02:40.593 * <redisgears_2> Hello log

使用 Redis 发布/订阅

如果您无法访问Redis数据库的日志文件,您可以使用发布/订阅功能。以下示例演示了如何使用client.call API发布到发布/订阅频道。

#!js api_version=1.0 name=lib

const logChannel = 'tfLogChannel'

function publog(client, message) {
  client.call('publish', logChannel, message)
}

redis.registerFunction('tflog', (client) => {
  publog(client, 'sample pub/sub log message')
  return 'sample'
})

在CLI会话中,订阅tfLogChannel频道并监听消息。

$ redis-cli
127.0.0.1:6379> subscribe tfLogChannel
1) "subscribe"
2) "tfLogChannel"
3) (integer) 1
Reading messages... (press Ctrl-C to quit or any key to type command)

在另一个CLI会话中,加载库,替换已经存在的内容,然后调用新函数:

127.0.0.1:6379> TFCALL lib.tflog 0
"sample"

你将在之前的CLI会话中看到以下内容:

1) "message"
2) "tfLogChannel"
3) "sample pub/sub log message"

使用发布/订阅有一个缺点。Redis 发布/订阅提供最多一次的消息传递语义,这意味着一旦消息被发送,它就不会再次发送。因此,如果消息未被消费,它将永远丢失。这对于调试来说可能是可以的,但从长远来看,redis.log 是日志持久化的更好解决方案。

RATE THIS PAGE
Back to top ↑