【问题标题】:How to log/debug a Crossbar guest worker?如何记录/调试 Crossbar 访客工作者?
【发布时间】:2015-01-31 12:18:31
【问题描述】:

如何从来宾工作人员那里捕获输出/日志和/或调试它? worker 是一个常规的 Python3 Autobahn WAMP 组件。无论我printraise 或写信给stderr 都无处可寻;或者我不知道在哪里看。仅当来宾因错误而终止时,才会将任何内容输出到 Crossbar 日志。配置如下:

workers:
  - type:       guest
    executable: python3
    arguments:  ['../foo.py']
    options:
      stdout: log
      stderr: log

stdoutstderr 选项似乎没有任何区别。

版本:

  • Crossbar.io:0.10.1
  • 高速公路|Python:0.9.5
  • worker:autobahn.asyncio.wamp.ApplicationSession

【问题讨论】:

    标签: autobahn crossbar


    【解决方案1】:

    默认情况下,Python stdout/stderr 是缓冲的,因此当您想到时,crossbar 不会看到来自客户机的日志。

    您可以设置环境变量PYTHONUNBUFFERED=1 或将-u 选项传递给您的解释器。

    【讨论】:

      【解决方案2】:

      crossbar check 验证 stdout: logstderr: log 但我不确定它为什么不记录错误。

      我通常会像下面那样做,但你的配置看起来更干净和方便。

      设置

      关注此 (Debugging Crossbar.io app in IntelliJ)。为您使用的 IDE 做类似的事情,但是,我建议使用 PyPy 作为交叉路由器而不是 CPython。

      记录

      关注此 (Logging WAMP worker Trace Back Error)。 您只需要启用 Trace Back Error 日志记录功能。您可以 import traceback 并使用 try except 或只使用创建一个简单的__init__ 函数,如下所示:

      class MyComponent(ApplicationSession):
         def __init__(self, config = None):
            ApplicationSession.__init__(self, config)
            self.traceback_app = True
      

      下面是一个完整的工作示例:

      错误日志

      .crossbar/config.yaml

      controller: {}
      workers:
      - realms:
        - name: realm1
          roles:
          - name: anonymous
            permissions:
            - {call: true, publish: true, register: true, subscribe: true, uri: '*'}
        transports:
        - endpoint: {port: 8080, type: tcp}
          paths:
            /: {directory: .., type: static}
            ws: {type: websocket}
          type: web
        type: router
      - arguments: [backend.py]
        executable: ../../../.pyenv/versions/autobahn/bin/python3
        options:
          watch:
            action: restart
            directories: [..]
          workdir: ..
        type: guest
      

      backend.py

      from autobahn.asyncio.wamp import ApplicationSession
      from autobahn import wamp
      
      from asyncio import coroutine
      import logging
      
      
      class MyComponent(ApplicationSession):
          def __init__(self, config = None):
              ApplicationSession.__init__(self, config)
              self.traceback_app = True
      
          @wamp.register("com.myapp.add2")
          def add2(self, x, y):
              if type(y) is str:
                  error = ' Sorry! Python can not add int with something else :('
                  logging.critical(error)
                  return error
      
              return x + y
      
          @coroutine
          def onJoin(self, details):
              res = yield from self.register(self)
              print("{} procedures registered.".format(len(res)))
      
      if __name__ == '__main__':
          from autobahn.asyncio.wamp import ApplicationRunner
      
          runner = ApplicationRunner(url="ws://localhost:8080/ws", realm="realm1")
          runner.run(MyComponent)
      

      frontend.html

      <!DOCTYPE html>
      <html lang="en">
      <head>
          <meta charset="UTF-8">
      </head>
      <body>
      <script>AUTOBAHN_DEBUG = false;</script>
      <script src="http://autobahn.s3.amazonaws.com/autobahnjs/latest/autobahn.min.jgz"></script>
      
      <script>
          var connection = new autobahn.Connection({
              url: "ws://localhost:8080/ws",
              realm: "realm1"
          });
      
          connection.onopen = function (session, details) {
              // Should work
              session.call("com.myapp.add2", [1, 2]).then(session.log);
      
              // Expected to log an error
              session.call("com.myapp.add2", [2, "three"]).then(session.log);
      
              // Expected to log the trace back error
              session.call("com.myapp.add2", ["one", 5]).then(session.log);
          };
      
          connection.onclose = function (reason, details) {
              console.log("Connection lost: " + reason);
          };
      
          connection.open();
      </script>
      </body>
      </html>
      

      【讨论】:

      • 我基本上是在 PyCharm 中使用调试运行配置自己启动 worker(反正我也在使用它),它的工作原理类似于 ... Py... Charm。 ^_^;;然而,stdout: log 选项应该用于什么以及为什么它不起作用仍然是个谜。我将把这个问题留待更长时间,也许会出现另一个建议......
      【解决方案3】:

      在初次启动后,我遇到了类似的交叉开关不输出任何内容的问题。即使代码中有 console.log() 。就我而言,它与 node.js 交叉开关路由器有关。

      解决方案是在启动命令中添加“--loglevel=debug”

      crossbar start --loglevel=debug
      

      然后我开始在执行 console.log() 时看到输出。

      【讨论】:

        猜你喜欢
        • 2011-01-20
        • 1970-01-01
        • 1970-01-01
        • 2021-04-26
        • 1970-01-01
        • 2016-12-02
        • 2016-04-30
        • 1970-01-01
        相关资源
        最近更新 更多