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

Ala*_*orm 0 node.js grpc grpc-js

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

$ GRPC_VERBOSITY=debug node index.js
Run Code Online (Sandbox Code Playgroud)

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

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

我已经研究了实现 的底层打字稿,@grpc/grpc-js我看到了日志调用的样子。

所以我的问题是:如何在@grpc/grpc-jsNodeJS 中使用NPM 包时启用日志记录

Ayu*_*pta 6

可以在这里看到:

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
  }
}
Run Code Online (Sandbox Code Playgroud)

设置GRPC_VERBOSITY=DEBUG为大写

另外,根据this,您必须设置GRPC_TRACE以指定要查看的日志类型。要查看所有日志,您可以将其设置为all.

所以,在你的情况下,命令变成

GRPC_VERBOSITY=DEBUG GRPC_TRACE=all node index.js
Run Code Online (Sandbox Code Playgroud)

  • 您可以根据[this](https://github.com/grpc/grpc-node/blob/master/packages/grpc-js/src/logging.ts)尝试`GRPC_VERBOSITY=DEBUG GRPC_TRACE=all node index.js` #L68)? (2认同)