From 94f477bc45e053a40b682aab923ec6ae417851ec Mon Sep 17 00:00:00 2001 From: hkc Date: Wed, 2 Nov 2022 20:11:38 +0300 Subject: [PATCH] Added configurable logging level --- README.md | 4 ++++ mastoposter/__main__.py | 4 ++-- mastoposter/sources.py | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 130ff1e..733104d 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,10 @@ connection error!!!) #### modules More about them later +#### loglevel +Self-explanatory, logging level. Can be either `DEBUG`, `INFO`, `WARNING` or +`ERROR`. Defaults to `INFO` + ### Modules There's two types of modules supported at this point: `telegram` and `discord`. Both of them are self-explanatory, but we'll go over them real quick. diff --git a/mastoposter/__main__.py b/mastoposter/__main__.py index ab38d11..371d4ac 100644 --- a/mastoposter/__main__.py +++ b/mastoposter/__main__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from asyncio import run from configparser import ConfigParser, ExtendedInterpolation -from logging import INFO, Formatter, StreamHandler, getLogger +from logging import INFO, Formatter, StreamHandler, getLevelName, getLogger from sys import stdout from mastoposter import execute_integrations, load_integrations_from from mastoposter.integrations import FilteredIntegration @@ -72,10 +72,10 @@ async def listen( def main(config_path: str): - init_logger() conf = ConfigParser(interpolation=ExtendedInterpolation()) conf.read(config_path) + init_logger(getLevelName(conf["main"].get("loglevel", "INFO"))) normalize_config(conf) modules: List[FilteredIntegration] = load_integrations_from(conf) diff --git a/mastoposter/sources.py b/mastoposter/sources.py index e2bdbb9..5865d46 100644 --- a/mastoposter/sources.py +++ b/mastoposter/sources.py @@ -1,10 +1,13 @@ from asyncio import exceptions from json import loads +from logging import getLogger from typing import AsyncGenerator from urllib.parse import urlencode from mastoposter.types import Status +logger = getLogger("sources") + async def websocket_source( url: str, reconnect: bool = False, **params @@ -22,6 +25,20 @@ async def websocket_source( raise Exception(event["error"]) if event["event"] == "update": yield Status.from_dict(loads(event["payload"])) - except (WebSocketException, TimeoutError, exceptions.TimeoutError): + else: + logger.warn("unknown event type %r", event["event"]) + logger.debug("data: %r", event) + except ( + WebSocketException, + TimeoutError, + exceptions.TimeoutError, + ) as e: if not reconnect: raise + else: + logger.warn("%r caught, reconnecting", e) + else: + logger.info( + "WebSocket closed connection without any errors, " + "but we're not done yet" + )