pantalaimon: Add propper logging support.

This commit is contained in:
Damir Jelić 2019-04-04 11:30:42 +02:00
parent 3fa6ce9292
commit f27eb836fe
4 changed files with 36 additions and 9 deletions

View File

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

View File

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

@ -0,0 +1,5 @@
import logbook
from logbook import Logger
logger = Logger("pantalaimon")
logger.level = logbook.WARNING

View File

@ -17,6 +17,7 @@ setup(
"aiohttp",
"appdirs",
"click",
"logbook",
"typing;python_version<'3.5'",
"matrix-nio @ git+https://github.com/poljar/matrix-nio.git@async#egg=matrix-nio-0"
],