crossbar check 验证 stdout: log 和 stderr: 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>