From fd05f39f4aa750bae09b707dec5754190fc486f3 Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 1 Nov 2022 13:52:40 +0300 Subject: [PATCH] Changed default level to ERROR, added TG API logs --- mastoposter/__init__.py | 2 +- mastoposter/__main__.py | 12 +++++++++--- mastoposter/integrations/telegram.py | 6 +++++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/mastoposter/__init__.py b/mastoposter/__init__.py index 1c4c717..8217e28 100644 --- a/mastoposter/__init__.py +++ b/mastoposter/__init__.py @@ -65,7 +65,7 @@ def load_integrations_from(config: ConfigParser) -> List[FilteredIntegration]: async def execute_integrations( status: Status, sinks: List[FilteredIntegration] ) -> List[Optional[str]]: - logger.debug("Executing integrations...") + logger.info("Executing integrations...") return await gather( *[sink[0](status) for sink in sinks if run_filters(sink[1], status)], return_exceptions=True, diff --git a/mastoposter/__main__.py b/mastoposter/__main__.py index 96ebfc8..a5c4ae7 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 DEBUG, Formatter, StreamHandler, getLogger +from logging import INFO, Formatter, StreamHandler, getLogger from sys import stdout from mastoposter import execute_integrations, load_integrations_from from mastoposter.integrations import FilteredIntegration @@ -19,9 +19,9 @@ VERIFY_CREDS_TEMPLATE = "https://{instance}/api/v1/accounts/verify_credentials" logger = getLogger() -def init_logger(loglevel: int = DEBUG): +def init_logger(loglevel: int = INFO): stdout_handler = StreamHandler(stdout) - stdout_handler.setLevel(DEBUG) + stdout_handler.setLevel(loglevel) formatter = Formatter("[%(asctime)s][%(levelname)5s:%(name)s] %(message)s") stdout_handler.setFormatter(formatter) logger.addHandler(stdout_handler) @@ -39,6 +39,12 @@ async def listen( async for status in source(**kwargs): logger.debug("Got status: %r", status) if status.account.id != user: + logger.info( + "Skipping status %s (account.id=%r != %r)", + status.uri, + status.account.id, + user, + ) continue # TODO: add option/filter to handle that diff --git a/mastoposter/integrations/telegram.py b/mastoposter/integrations/telegram.py index 4108dd3..3a3f185 100644 --- a/mastoposter/integrations/telegram.py +++ b/mastoposter/integrations/telegram.py @@ -88,9 +88,13 @@ class TelegramIntegration(BaseIntegration): async def _tg_request(self, method: str, **kwargs) -> TGResponse: url = API_URL.format(self.token, method) async with AsyncClient() as client: - return TGResponse.from_dict( + response = TGResponse.from_dict( (await client.post(url, json=kwargs)).json(), kwargs ) + if not response.ok: + logger.error("TG error: %r", response.error) + logger.info("parameters: %r", kwargs) + return response async def _post_plaintext(self, text: str) -> TGResponse: logger.debug("Sending HTML message: %r", text)