add clean logging

This commit is contained in:
histausse 2021-10-03 20:07:14 +02:00
parent d9528228ba
commit 46dc303d9a
Signed by: histausse
GPG key ID: 67486F107F62E9E9
3 changed files with 18 additions and 9 deletions

View file

@ -1,3 +1,5 @@
# matrix-bot
A package implementing a reusable class to make matrix bots
A package implementing a reusable class to make matrix bots
This is inventing the wheel once again, but who care? It's fun :)

View file

@ -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

View file

@ -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(