From dc8cd290b6b0fdfeeb95b4cfa6a51ec378895e8a Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 13 Jun 2025 21:43:45 +0000 Subject: [PATCH] diag: Optimize logging in bybit exe gw plugin * Format params as json (not python pseudo json) * Format params lazily --- .../bybit_execution_gateway.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/lib_python_interop/python_interop_plugins/bybit_execution_gateway.py b/src/lib_python_interop/python_interop_plugins/bybit_execution_gateway.py index 3d0b81061..f39dba1a8 100644 --- a/src/lib_python_interop/python_interop_plugins/bybit_execution_gateway.py +++ b/src/lib_python_interop/python_interop_plugins/bybit_execution_gateway.py @@ -196,7 +196,7 @@ class BybitExecutionGateway(ExecutionGateway): "channel_type": "private", "trace_logging": False, } - self.logger.info("[bybit_gw_stream_sub.json]%s" % json.dumps(params)) + self.logger.info("[bybit_gw_stream_sub.json]%s" % JsonElement(params)) self.stream_cli = WebSocket(**params) self.stream_cli.order_stream(on_order_update) self.stream_cli.execution_stream(on_exe_report) @@ -206,7 +206,7 @@ class BybitExecutionGateway(ExecutionGateway): "api_secret": self.secret_key, "testnet": self.use_testnet, } - self.logger.info("[bybit_gw_start_trd_cli.json]%s" % json.dumps(params)) + self.logger.info("[bybit_gw_start_trd_cli.json]%s" % JsonElement(params)) self.trade_cli = WebSocketTrading(**params) params = { "category": "linear", @@ -229,7 +229,7 @@ class BybitExecutionGateway(ExecutionGateway): logging.INFO if all(map(lambda kv: kv[1], conn_statuses.items())) else logging.WARNING, - "[bybit_gw_connection_status.json]%s" % JsonElement(conn_statuses), + "[bybit_gw_connection_status.json]%s" % SLog(**conn_statuses), ) def start(self, bridge: ExecutionGatewayBridge): @@ -241,7 +241,7 @@ class BybitExecutionGateway(ExecutionGateway): '[bybit_gw_start_err.json]{ "err": "%s" }' % str(e), exc_info=True ) - self.logger.info("[bybit_gw_start.json]%s" % json.dumps({"ts": time.time_ns()})) + self.logger.info("[bybit_gw_start.json]%s" % SLog()) self.bridge = bridge threading.Thread( target=start_wrapper, @@ -250,11 +250,11 @@ class BybitExecutionGateway(ExecutionGateway): ).start() def stop(self): - self.logger.info("[bybit_gw_stop.json]%s" % json.dumps({"ts": time.time_ns()})) + self.logger.info("[bybit_gw_stop.json]%s" % SLog()) self.stop_event.set() def create_order(self, req: PyCreateOrderRequest): - self.logger.info("[bybit_gw_crt_ord.json]%s" % str(req)) + self.logger.info("[bybit_gw_crt_ord.json]%s" % SLog(req=req)) if not self.stream_cli or not self.trade_cli or not self.bridge: self._on_abort( req.key(), @@ -283,11 +283,11 @@ class BybitExecutionGateway(ExecutionGateway): "price": format_price(sec, px), "qty": format_quantity(sec, qty), } - self.logger.info("[bybit_gw_crt_ord_send.json]%s" % json.dumps(params)) + self.logger.info("[bybit_gw_crt_ord_send.json]%s" % SLog(params=params)) self.trade_cli.place_order(lambda msg: self._on_order_ack("crt", msg), **params) def cancel_replace_order(self, req: PyCancelReplaceOrderRequest): - self.logger.info("[bybit_gw_cxlrep_ord.json]%s" % req) + self.logger.info("[bybit_gw_cxlrep_ord.json]%s" % SLog(req=req)) if not self.stream_cli or not self.trade_cli: self._on_abort(req.key(), "cxlrep", "WebSocket client not created.") return @@ -307,12 +307,13 @@ class BybitExecutionGateway(ExecutionGateway): "price": format_price(sec, req.price()), "qty": format_quantity(sec, req.quantity()), } + self.logger.info("[bybit_gw_cxlrep_ord_send.json]%s" % SLog(params=params)) self.trade_cli.amend_order( lambda msg: self._on_order_ack("cxlrep", msg), **params ) def cancel_order(self, req: PyCancelOrderRequest): - self.logger.info("[bybit_gw_cxl_ord.json]%s" % str(req)) + self.logger.info("[bybit_gw_cxl_ord.json]%s" % SLog(req=req)) if not self.stream_cli or not self.trade_cli: self._on_abort(req.key(), "cxl", "WebSocket client not created.") return @@ -326,12 +327,11 @@ class BybitExecutionGateway(ExecutionGateway): "category": "linear", "symbol": req.exchange_security_id(), } + self.logger.info("[bybit_gw_cxl_ord_send.json]%s" % SLog(params=params)) self.trade_cli.cancel_order( lambda msg: self._on_order_ack("cxl", msg), **params ) def on_securities(self, securities: PySecurityList): - self.logger.info( - "[bybit_gw_sec.json][%s]" % ", ".join([str(sec) for sec in securities]) - ) + self.logger.info("[bybit_gw_sec.json]%s" % SLog(secs=securities)) self.securities = {sec.name(): sec for sec in securities}