From 95c9a523e019629f59847f8f6a0c44de5d24ad50 Mon Sep 17 00:00:00 2001 From: hkc Date: Tue, 1 Nov 2022 13:37:47 +0300 Subject: [PATCH] More logging? --- mastoposter/__main__.py | 3 ++- mastoposter/filters/__init__.py | 2 +- mastoposter/integrations/discord/__init__.py | 6 ++++++ mastoposter/integrations/telegram.py | 20 +++++++++++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/mastoposter/__main__.py b/mastoposter/__main__.py index 4073ab0..96ebfc8 100644 --- a/mastoposter/__main__.py +++ b/mastoposter/__main__.py @@ -19,12 +19,13 @@ VERIFY_CREDS_TEMPLATE = "https://{instance}/api/v1/accounts/verify_credentials" logger = getLogger() -def init_logger(): +def init_logger(loglevel: int = DEBUG): stdout_handler = StreamHandler(stdout) stdout_handler.setLevel(DEBUG) formatter = Formatter("[%(asctime)s][%(levelname)5s:%(name)s] %(message)s") stdout_handler.setFormatter(formatter) logger.addHandler(stdout_handler) + logger.setLevel(loglevel) async def listen( diff --git a/mastoposter/filters/__init__.py b/mastoposter/filters/__init__.py index 21398bd..015245c 100644 --- a/mastoposter/filters/__init__.py +++ b/mastoposter/filters/__init__.py @@ -15,7 +15,7 @@ logger = getLogger(__name__) def run_filters(filters: List[FilterInstance], status: Status) -> bool: - logger.debug("Running filters on %r", status) + logger.debug("Running filters on %r", status.id) if not filters: logger.debug("No filters, returning True") diff --git a/mastoposter/integrations/discord/__init__.py b/mastoposter/integrations/discord/__init__.py index 417c9dd..bc674d4 100644 --- a/mastoposter/integrations/discord/__init__.py +++ b/mastoposter/integrations/discord/__init__.py @@ -1,4 +1,5 @@ from configparser import SectionProxy +from logging import getLogger from typing import List, Optional from httpx import AsyncClient from zlib import crc32 @@ -10,6 +11,8 @@ from mastoposter.integrations.discord.types import ( ) from mastoposter.types import Status +logger = getLogger(__name__) + class DiscordIntegration(BaseIntegration): def __init__(self, webhook: str): @@ -35,6 +38,9 @@ class DiscordIntegration(BaseIntegration): if embeds is not None else [], } + + logger.debug("Executing webhook with %r", json) + return ( await c.post( self.webhook, diff --git a/mastoposter/integrations/telegram.py b/mastoposter/integrations/telegram.py index fb81b07..4108dd3 100644 --- a/mastoposter/integrations/telegram.py +++ b/mastoposter/integrations/telegram.py @@ -1,5 +1,6 @@ from configparser import SectionProxy from dataclasses import dataclass +from logging import getLogger from typing import Any, List, Mapping, Optional from httpx import AsyncClient from jinja2 import Template @@ -8,6 +9,9 @@ from mastoposter.types import Attachment, Poll, Status from emoji import emojize +logger = getLogger(__name__) + + @dataclass class TGResponse: ok: bool @@ -89,6 +93,7 @@ class TelegramIntegration(BaseIntegration): ) async def _post_plaintext(self, text: str) -> TGResponse: + logger.debug("Sending HTML message: %r", text) return await self._tg_request( "sendMessage", parse_mode="HTML", @@ -101,6 +106,9 @@ class TelegramIntegration(BaseIntegration): async def _post_media(self, text: str, media: Attachment) -> TGResponse: # Just to be safe if media.type not in MEDIA_MAPPING: + logger.warning( + "Media %r has unknown type, falling back to plaintext", media + ) return await self._post_plaintext(text) return await self._tg_request( @@ -116,6 +124,7 @@ class TelegramIntegration(BaseIntegration): async def _post_mediagroup( self, text: str, media: List[Attachment] ) -> TGResponse: + logger.debug("Sendind media group: %r (text=%r)", media, text) media_list: List[dict] = [] allowed_medias = {"image", "gifv", "video", "audio", "unknown"} for attachment in media: @@ -149,6 +158,7 @@ class TelegramIntegration(BaseIntegration): async def _post_poll( self, poll: Poll, reply_to: Optional[str] = None ) -> TGResponse: + logger.debug("Sending poll: %r", poll) return await self._tg_request( "sendPoll", disable_notification=self.silent, @@ -198,9 +208,17 @@ class TelegramIntegration(BaseIntegration): return str.join(",", map(str, ids)) def __repr__(self) -> str: + bot_uid, key = self.token.split(":") return ( "" - ).format(chat=self.chat_id, silent=self.silent, template=self.template) + ).format( + chat=self.chat_id, + silent=self.silent, + template=self.template, + bot_uid=bot_uid, + key=str.join("", ("X" for _ in key)), + )