From 20bdbf9cd121efae64a0506b3f0197b6a8ce7b2a Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Tue, 10 May 2022 01:10:47 -0400 Subject: [PATCH] Add config for logging arguments of RPC commands --- matrix_appservice_kakaotalk/config.py | 1 + matrix_appservice_kakaotalk/example-config.yaml | 5 +++++ matrix_appservice_kakaotalk/rpc/rpc.py | 10 ++++++++-- node/src/client.js | 12 ++++++++++-- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/matrix_appservice_kakaotalk/config.py b/matrix_appservice_kakaotalk/config.py index a6afc00..54d1ec7 100644 --- a/matrix_appservice_kakaotalk/config.py +++ b/matrix_appservice_kakaotalk/config.py @@ -117,6 +117,7 @@ class Config(BaseBridgeConfig): else: copy("rpc.connection.host") copy("rpc.connection.port") + copy("rpc.logging_keys") def _get_permissions(self, key: str) -> tuple[bool, bool, bool, str]: level = self["bridge.permissions"].get(key, "") diff --git a/matrix_appservice_kakaotalk/example-config.yaml b/matrix_appservice_kakaotalk/example-config.yaml index b7b8c30..e8938d2 100644 --- a/matrix_appservice_kakaotalk/example-config.yaml +++ b/matrix_appservice_kakaotalk/example-config.yaml @@ -252,6 +252,11 @@ rpc: # Only for type: tcp host: localhost port: 29392 + # Command arguments to print in logs. Optional. + # TODO Support nested arguments, like channel_props.ktid + logging_keys: + - mxid + #- channel_props # Python logging configuration. # diff --git a/matrix_appservice_kakaotalk/rpc/rpc.py b/matrix_appservice_kakaotalk/rpc/rpc.py index 580fca1..c90ef7f 100644 --- a/matrix_appservice_kakaotalk/rpc/rpc.py +++ b/matrix_appservice_kakaotalk/rpc/rpc.py @@ -86,6 +86,7 @@ class RPCClient: _is_connected: CancelableEvent _is_disconnected: CancelableEvent _connection_lock: asyncio.Lock + _logging_keys: list[str] def __init__(self, config: Config, register_config_key: str) -> None: self.config = config @@ -105,6 +106,7 @@ class RPCClient: self._is_disconnected = CancelableEvent(self.loop) self._is_disconnected.set() self._connection_lock = asyncio.Lock() + self._logging_keys = config["rpc.logging_keys"] async def connect(self) -> None: async with self._connection_lock: @@ -147,7 +149,8 @@ class RPCClient: self._read_task = self.loop.create_task(self._try_read_loop()) await self._raw_request("register", peer_id=self.config["appservice.address"], - register_config=self.config[self.register_config_key]) + register_config=self.config[self.register_config_key], + logging_keys=self._logging_keys) self._is_connected.set() self._is_disconnected.clear() @@ -302,7 +305,10 @@ class RPCClient: req_id = self._next_req_id future = self._response_waiters[req_id] = self.loop.create_future() req = {"id": req_id, "command": command, **data} - self.log.debug("Request %d: %s", req_id, command) + self.log.debug("Request %d: %s", req_id, + ', '.join( + [command] + + [f"{k}: {data[k]}" for k in self._logging_keys if k in data])) assert self._writer is not None self._writer.write(json.dumps(req).encode("utf-8")) self._writer.write(b"\n") diff --git a/node/src/client.js b/node/src/client.js index b5889d9..e2f7ee2 100644 --- a/node/src/client.js +++ b/node/src/client.js @@ -424,7 +424,8 @@ export default class PeerClient { this.maxCommandID = 0 this.peerID = null this.deviceName = "KakaoTalk Bridge" - + /** @type {[string]} */ + this.loggingKeys = [] /** @type {Map} */ this.userClients = new Map() } @@ -1166,10 +1167,12 @@ export default class PeerClient { * @param {string} req.peer_id * @param {Object} req.register_config * @param {string} req.register_config.device_name + * @param {?[string]} req.logging_keys */ handleRegister = async (req) => { this.peerID = req.peer_id this.deviceName = req.register_config.device_name || this.deviceName + this.loggingKeys = req.logging_keys || this.loggingKeys this.log(`Registered socket ${this.connID} -> ${this.peerID}`) if (this.manager.clients.has(this.peerID)) { const oldClient = this.manager.clients.get(this.peerID) @@ -1200,7 +1203,12 @@ export default class PeerClient { this.log("Ignoring old request", req.id) return } - this.log("Received request", req.id, "with command", req.command) + this.log( + `Request ${req.id}:`, + [req.command].concat( + this.loggingKeys.filter(k => k in req).map(k => `${k}: ${JSON.stringify(req[k], this.#writeReplacer)}`)) + .join(', ') + ) this.maxCommandID = req.id let handler if (!this.peerID) {