mirror of
https://github.com/matrix-org/pantalaimon.git
synced 2024-10-01 03:35:38 -04:00
pantalaimon: Add propper logging support.
This commit is contained in:
parent
3fa6ce9292
commit
f27eb836fe
@ -1,4 +1,5 @@
|
||||
from typing import Any, Dict
|
||||
from pprint import pformat
|
||||
|
||||
from nio import (
|
||||
AsyncClient,
|
||||
@ -8,6 +9,8 @@ from nio import (
|
||||
SyncResponse
|
||||
)
|
||||
|
||||
from pantalaimon.log import logger
|
||||
|
||||
|
||||
class PantaClient(AsyncClient):
|
||||
"""A wrapper class around a nio AsyncClient extending its functionality."""
|
||||
@ -23,26 +26,28 @@ class PantaClient(AsyncClient):
|
||||
"""
|
||||
for room_id, room_dict in body["rooms"]["join"].items():
|
||||
if not self.rooms[room_id].encrypted:
|
||||
print("Room {} not encrypted skipping...".format(
|
||||
logger.info("Room {} is not encrypted skipping...".format(
|
||||
self.rooms[room_id].display_name
|
||||
))
|
||||
continue
|
||||
|
||||
for event in room_dict["timeline"]["events"]:
|
||||
if event["type"] != "m.room.encrypted":
|
||||
print("Event not encrypted skipping...")
|
||||
logger.info("Event is not encrypted: "
|
||||
"{}".format(pformat(event)))
|
||||
continue
|
||||
|
||||
parsed_event = RoomEncryptedEvent.parse_event(event)
|
||||
parsed_event.room_id = room_id
|
||||
|
||||
if not isinstance(parsed_event, MegolmEvent):
|
||||
print("Not a megolm event.")
|
||||
logger.warn("Encrypted event is not a megolm event:"
|
||||
"{}".format(pformat(event)))
|
||||
continue
|
||||
|
||||
try:
|
||||
decrypted_event = self.decrypt_event(parsed_event)
|
||||
print("Decrypted event: {}".format(decrypted_event))
|
||||
logger.info("Decrypted event: {}".format(decrypted_event))
|
||||
event["type"] = "m.room.message"
|
||||
|
||||
# TODO support other event types
|
||||
@ -63,7 +68,7 @@ class PantaClient(AsyncClient):
|
||||
event["verified"] = decrypted_event.verified
|
||||
|
||||
except EncryptionError as error:
|
||||
print("ERROR decrypting {}".format(error))
|
||||
logger.warn(error)
|
||||
continue
|
||||
|
||||
return body
|
||||
|
@ -9,6 +9,7 @@ import json
|
||||
import click
|
||||
from ipaddress import ip_address
|
||||
from urllib.parse import urlparse
|
||||
from logbook import StderrHandler
|
||||
|
||||
from aiohttp import web, ClientSession
|
||||
from nio import (
|
||||
@ -22,6 +23,7 @@ from json import JSONDecodeError
|
||||
from multidict import CIMultiDict
|
||||
|
||||
from pantalaimon.client import PantaClient
|
||||
from pantalaimon.log import logger
|
||||
|
||||
|
||||
@attr.s
|
||||
@ -242,9 +244,8 @@ class ProxyDaemon:
|
||||
if device.deleted:
|
||||
continue
|
||||
|
||||
print("Automatically verifying device {}".format(
|
||||
device.id
|
||||
))
|
||||
logger.info("Automatically verifying device {} of "
|
||||
"user {}".format(device.id, user_id))
|
||||
client.verify_device(device)
|
||||
|
||||
json_response = await response.transport_response.json()
|
||||
@ -387,13 +388,28 @@ class ipaddress(click.ParamType):
|
||||
default=8009,
|
||||
help="The listening port for incoming client connections (default: 8009)"
|
||||
)
|
||||
@click.option("--log-level", type=click.Choice([
|
||||
"error",
|
||||
"warning",
|
||||
"info",
|
||||
"debug"
|
||||
]), default="error")
|
||||
@click.argument(
|
||||
"homeserver",
|
||||
type=URL(),
|
||||
)
|
||||
def main(proxy, ssl_insecure, listen_address, listen_port, homeserver):
|
||||
def main(
|
||||
proxy,
|
||||
ssl_insecure,
|
||||
listen_address,
|
||||
listen_port,
|
||||
log_level,
|
||||
homeserver
|
||||
):
|
||||
ssl = None if ssl_insecure is False else False
|
||||
|
||||
StderrHandler(level=log_level.upper()).push_application()
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
proxy, app = loop.run_until_complete(init(
|
||||
homeserver.geturl(),
|
||||
|
5
pantalaimon/log.py
Normal file
5
pantalaimon/log.py
Normal file
@ -0,0 +1,5 @@
|
||||
import logbook
|
||||
from logbook import Logger
|
||||
|
||||
logger = Logger("pantalaimon")
|
||||
logger.level = logbook.WARNING
|
Loading…
Reference in New Issue
Block a user