NodeJS:如何为@grpc/grpc.js 包启用日志记录

NodeJS: How Is Logging Enabled for the @grpc/grpc.js Package

在 NodeJS 中创建 GRPC 客户端时,如果我使用 grpc package,我可以在 GRPC_VERBOSITY 设置为 debug 环境变量的情况下启动我的程序。

$ GRPC_VERBOSITY=debug node index.js

我将获得大量有关正在发生的网络通信的信息。

但是,如果我切换实现并使用 @grpc/grpc-js 包,GRPC_VERBOSITY=debug 没有效果。

我查看了实现 @grpc/grpc-js 的底层打字稿,我看到了 logging calls 的样子。

所以我的问题是:在将 @grpc/grpc-js NPM 包与 NodeJS

一起使用时如何启用日志记录

可以看出here:

if (process.env.GRPC_VERBOSITY) {
  switch (process.env.GRPC_VERBOSITY) {
    case 'DEBUG':
      _logVerbosity = LogVerbosity.DEBUG;
      break;
    case 'INFO':
      _logVerbosity = LogVerbosity.INFO;
      break;
    case 'ERROR':
      _logVerbosity = LogVerbosity.ERROR;
      break;
    default:
    // Ignore any other values
  }
}

GRPC_VERBOSITY=DEBUG设为大写

另外,根据this,你必须设置GRPC_TRACE来指定你想看到什么样的日志。要查看所有日志,您可以将其设置为 all.

因此,在您的情况下,命令变为

GRPC_VERBOSITY=DEBUG GRPC_TRACE=all node index.js