如何从来宾工作中捕获输出/日志和/或对其进行调试?该worker是一个常规的Python3 Autobahn WAMP组件。无论我print
或raise
还是写stderr
都无处可寻;或者我不知道在哪里看。仅当guest虚拟机以错误终止时,才会输出到Crossbar日志。这是配置:
workers:
- type: guest
executable: python3
arguments: ['../foo.py']
options:
stdout: log
stderr: log
stdout
和stderr
选项似乎没有任何区别。
版本:
答案 0 :(得分:1)
crossbar check
验证stdout: log
和stderr: log
,但我不确定为什么它不会记录错误。
我通常会像下面这样做,但你的配置看起来更干净,更方便。
<强>设置强>
遵循此(Debugging Crossbar.io app in IntelliJ)。对你使用的IDE做类似的事情,但是,我建议使用PyPy代替CPython而不是CPython。
<强>登录强>
遵循此(Logging WAMP worker 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>
答案 1 :(得分:1)
默认情况下,Python stdout / stderr被缓冲,因此当你想到时,crossbar不会看到来自guest虚拟机的日志。
您可以设置环境变量PYTHONUNBUFFERED=1
或将-u
选项传递给解释器。
答案 2 :(得分:0)
我遇到类似的交叉开关问题,在初始启动后不输出任何东西。即使代码中包含console.log()。就我而言,它是与node.js纵横制路由器一起使用的。
解决方案是在启动命令中添加“ --loglevel = debug”
crossbar start --loglevel=debug
然后我在执行console.log()时开始看到输出。