From 46dc303d9af5c7cee55d7d2b1bf16236ddbc24fc Mon Sep 17 00:00:00 2001 From: Jean-Marie Mineau Date: Sun, 3 Oct 2021 20:07:14 +0200 Subject: [PATCH] add clean logging --- README.md | 4 +++- src/matrix_bot/client.py | 20 ++++++++++++-------- tests/test.py | 3 +++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 5ecdcc1..e4c8077 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # matrix-bot -A package implementing a reusable class to make matrix bots \ No newline at end of file +A package implementing a reusable class to make matrix bots + +This is inventing the wheel once again, but who care? It's fun :) diff --git a/src/matrix_bot/client.py b/src/matrix_bot/client.py index 0187dad..392a178 100644 --- a/src/matrix_bot/client.py +++ b/src/matrix_bot/client.py @@ -4,6 +4,7 @@ Connect to the matrix server and handle interactions with the server. """ import asyncio +import logging import nio from aiopath import AsyncPath from typing import ( @@ -18,6 +19,8 @@ from .utils import ( RoomId ) +log = logging.getLogger(__name__) + class Client(Aobject): """ Connect to the matrix server and handle interactions with the @@ -76,6 +79,7 @@ class Client(Aobject): resp = await self.__client.login(password) if isinstance(resp, nio.responses.LoginError): raise RuntimeError(f"Fail to connect: {resp.message}") + log.info("logged in") self.whitelist_rooms = None if whitelist_rooms_names: @@ -146,26 +150,26 @@ class Client(Aobject): if self.whitelist_rooms is not None: if room_id not in self.whitelist_rooms: - print(f"Received invite for {room_id}, but room_id is not in the white list.") # TODO: better logging + log.warning(f"Received invite for {room_id}, but room_id is not in the white list.") else: accept_invite = True - print(f"Received invite for {room_id}: invite accepted.") # TODO: better logging + log.info(f"Received invite for {room_id}: invite accepted.") else: accept_invite = True - print(f"Received invite for {room_id}: invite accepted.") # TODO: better logging + log.info(f"Received invite for {room_id}: invite accepted.") if accept_invite: result = await self.__client.join(room_id) if isinstance(result, nio.JoinError): - print(f"Error while joinning room {room_id}: {result.message}") # TODO: better logging + log.warning(f"Error while joinning room {room_id}: {result.message}") else: - print(f"{room_id} joined") # TODO: better logging + log.info(f"{room_id} joined") else: result = await self.__client.room_leave(room_id) if isinstance(result, nio.RoomLeaveError): - print(f"Error while leaving room {room_id}: {result.message}") # TODO: better logging + log.warning(f"Error while leaving room {room_id}: {result.message}") else: - print(f"{room_id} left") # TODO: better logging + log.info(f"{room_id} left") self.__invites_queue.task_done() @@ -183,7 +187,7 @@ class Client(Aobject): since=self.__sync_token ) if isinstance(sync_resp, nio.responses.SyncError): - print(f"Error while syncronizing: {sync_resp.message}") # TODO: use proper logging + log.warning(f"Error while syncronizing: {sync_resp.message}") continue self.__sync_token = sync_resp.next_batch diff --git a/tests/test.py b/tests/test.py index c5ea0c3..104442e 100644 --- a/tests/test.py +++ b/tests/test.py @@ -3,11 +3,14 @@ Not really tests, because I don't know how to testt without a whole matrix serve """ import asyncio +import logging import os from dotenv import load_dotenv from matrix_bot.client import Client from getpass import getpass +logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG) + async def main(): load_dotenv() client = await Client(