add clean logging
This commit is contained in:
parent
d9528228ba
commit
46dc303d9a
3 changed files with 18 additions and 9 deletions
|
@ -1,3 +1,5 @@
|
||||||
# matrix-bot
|
# 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 :)
|
||||||
|
|
|
@ -4,6 +4,7 @@ Connect to the matrix server and handle interactions with the server.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
import nio
|
import nio
|
||||||
from aiopath import AsyncPath
|
from aiopath import AsyncPath
|
||||||
from typing import (
|
from typing import (
|
||||||
|
@ -18,6 +19,8 @@ from .utils import (
|
||||||
RoomId
|
RoomId
|
||||||
)
|
)
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
class Client(Aobject):
|
class Client(Aobject):
|
||||||
"""
|
"""
|
||||||
Connect to the matrix server and handle interactions with the
|
Connect to the matrix server and handle interactions with the
|
||||||
|
@ -76,6 +79,7 @@ class Client(Aobject):
|
||||||
resp = await self.__client.login(password)
|
resp = await self.__client.login(password)
|
||||||
if isinstance(resp, nio.responses.LoginError):
|
if isinstance(resp, nio.responses.LoginError):
|
||||||
raise RuntimeError(f"Fail to connect: {resp.message}")
|
raise RuntimeError(f"Fail to connect: {resp.message}")
|
||||||
|
log.info("logged in")
|
||||||
|
|
||||||
self.whitelist_rooms = None
|
self.whitelist_rooms = None
|
||||||
if whitelist_rooms_names:
|
if whitelist_rooms_names:
|
||||||
|
@ -146,26 +150,26 @@ class Client(Aobject):
|
||||||
|
|
||||||
if self.whitelist_rooms is not None:
|
if self.whitelist_rooms is not None:
|
||||||
if room_id not in self.whitelist_rooms:
|
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:
|
else:
|
||||||
accept_invite = True
|
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:
|
else:
|
||||||
accept_invite = True
|
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:
|
if accept_invite:
|
||||||
result = await self.__client.join(room_id)
|
result = await self.__client.join(room_id)
|
||||||
if isinstance(result, nio.JoinError):
|
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:
|
else:
|
||||||
print(f"{room_id} joined") # TODO: better logging
|
log.info(f"{room_id} joined")
|
||||||
else:
|
else:
|
||||||
result = await self.__client.room_leave(room_id)
|
result = await self.__client.room_leave(room_id)
|
||||||
if isinstance(result, nio.RoomLeaveError):
|
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:
|
else:
|
||||||
print(f"{room_id} left") # TODO: better logging
|
log.info(f"{room_id} left")
|
||||||
|
|
||||||
self.__invites_queue.task_done()
|
self.__invites_queue.task_done()
|
||||||
|
|
||||||
|
@ -183,7 +187,7 @@ class Client(Aobject):
|
||||||
since=self.__sync_token
|
since=self.__sync_token
|
||||||
)
|
)
|
||||||
if isinstance(sync_resp, nio.responses.SyncError):
|
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
|
continue
|
||||||
|
|
||||||
self.__sync_token = sync_resp.next_batch
|
self.__sync_token = sync_resp.next_batch
|
||||||
|
|
|
@ -3,11 +3,14 @@ Not really tests, because I don't know how to testt without a whole matrix serve
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from matrix_bot.client import Client
|
from matrix_bot.client import Client
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
|
|
||||||
|
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.DEBUG)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
client = await Client(
|
client = await Client(
|
||||||
|
|
Loading…
Reference in a new issue