Add some logging

This commit is contained in:
jeltz 2021-03-15 23:20:54 +01:00
parent ca26257b20
commit f211eb32c5

12
bot.py
View file

@ -2,6 +2,7 @@
import argparse import argparse
import asyncio import asyncio
import dataclasses import dataclasses
import logging
import aiohttp.web import aiohttp.web
import jinja2 import jinja2
@ -73,11 +74,11 @@ async def format_events(config, src, dest):
while True: while True:
event = await src.get() event = await src.get()
print(event) logging.debug("Formatting message: %s", event)
try: try:
template = env.from_string(TEMPLATES[event["event_name"]]) template = env.from_string(TEMPLATES[event["event_name"]])
except KeyError: except KeyError:
print("Unknown message type:", event["event_name"]) logging.info("Unknown message type: %s", event["event_name"])
continue continue
rendered = template.render(**event) rendered = template.render(**event)
@ -107,7 +108,8 @@ async def run_app(config, dest):
async def webhook(request): async def webhook(request):
tokens = request.query.getall("token", []) tokens = request.query.getall("token", [])
if config.webhook_token not in tokens: if config.webhook_token not in tokens:
raise aiohttp.web.HTTPForbidden("Invalid token") logging.warning("Invalid tokens: %s", tokens)
raise aiohttp.web.HTTPForbidden("Invalid tokens")
event = await request.json() event = await request.json()
await dest.put(event) await dest.put(event)
@ -123,6 +125,8 @@ async def run_app(config, dest):
async def main(): async def main():
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config-file", default="config.yaml") parser.add_argument("-c", "--config-file", default="config.yaml")
@ -136,6 +140,8 @@ async def main():
events = asyncio.Queue() events = asyncio.Queue()
formatted = asyncio.Queue() formatted = asyncio.Queue()
logging.info("Started Kanbot")
tasks = ( tasks = (
asyncio.create_task(run_app(config, events)), asyncio.create_task(run_app(config, events)),
asyncio.create_task(format_events(config, events, formatted)), asyncio.create_task(format_events(config, events, formatted)),