Improve logging

This commit is contained in:
2024-07-30 02:04:33 +02:00
parent 3004da58ce
commit dc46a402eb
5 changed files with 19 additions and 25 deletions

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import json import json
import logging
from dataclasses import dataclass from dataclasses import dataclass
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -9,11 +10,13 @@ from markdownify import markdownify
from reader import Entry, Feed, Reader, TagNotFoundError from reader import Entry, Feed, Reader, TagNotFoundError
from discord_rss_bot.is_url_valid import is_url_valid from discord_rss_bot.is_url_valid import is_url_valid
from discord_rss_bot.settings import get_reader, logger from discord_rss_bot.settings import get_reader
if TYPE_CHECKING: if TYPE_CHECKING:
from reader.types import JSONType from reader.types import JSONType
logger: logging.Logger = logging.getLogger(__name__)
@dataclass(slots=True) @dataclass(slots=True)
class CustomEmbed: class CustomEmbed:
@ -132,7 +135,7 @@ def get_first_image(summary: str | None, content: str | None) -> str:
if content and (images := BeautifulSoup(content, features="lxml").find_all("img")): if content and (images := BeautifulSoup(content, features="lxml").find_all("img")):
for image in images: for image in images:
if not is_url_valid(image.attrs["src"]): if not is_url_valid(image.attrs["src"]):
logger.warning(f"Invalid URL: {image.attrs['src']}") logger.warning("Invalid URL: %s", image.attrs["src"])
continue continue
# Genshins first image is a divider, so we ignore it. # Genshins first image is a divider, so we ignore it.
@ -141,7 +144,7 @@ def get_first_image(summary: str | None, content: str | None) -> str:
if summary and (images := BeautifulSoup(summary, features="lxml").find_all("img")): if summary and (images := BeautifulSoup(summary, features="lxml").find_all("img")):
for image in images: for image in images:
if not is_url_valid(image.attrs["src"]): if not is_url_valid(image.attrs["src"]):
logger.warning(f"Invalid URL: {image.attrs['src']}") logger.warning("Invalid URL: %s", image.attrs["src"])
continue continue
# Genshins first image is a divider, so we ignore it. # Genshins first image is a divider, so we ignore it.
@ -220,7 +223,6 @@ def replace_tags_in_embed(feed: Feed, entry: Entry) -> CustomEmbed:
{"{{entry_updated}}": entry_updated or ""}, {"{{entry_updated}}": entry_updated or ""},
{"{{image_1}}": first_image or ""}, {"{{image_1}}": first_image or ""},
] ]
for replacement in list_of_replacements: for replacement in list_of_replacements:
for template, replace_with in replacement.items(): for template, replace_with in replacement.items():
embed.title = try_to_replace(embed.title, template, replace_with) embed.title = try_to_replace(embed.title, template, replace_with)

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import datetime import datetime
import logging
import pprint import pprint
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
@ -12,13 +13,15 @@ from discord_rss_bot import custom_message
from discord_rss_bot.filter.blacklist import should_be_skipped from discord_rss_bot.filter.blacklist import should_be_skipped
from discord_rss_bot.filter.whitelist import has_white_tags, should_be_sent from discord_rss_bot.filter.whitelist import has_white_tags, should_be_sent
from discord_rss_bot.is_url_valid import is_url_valid from discord_rss_bot.is_url_valid import is_url_valid
from discord_rss_bot.settings import default_custom_message, get_reader, logger from discord_rss_bot.settings import default_custom_message, get_reader
if TYPE_CHECKING: if TYPE_CHECKING:
from collections.abc import Iterable from collections.abc import Iterable
from requests import Response from requests import Response
logger: logging.Logger = logging.getLogger(__name__)
def send_entry_to_discord(entry: Entry, custom_reader: Reader | None = None) -> str | None: def send_entry_to_discord(entry: Entry, custom_reader: Reader | None = None) -> str | None:
"""Send a single entry to Discord. """Send a single entry to Discord.
@ -186,11 +189,11 @@ def send_to_discord(custom_reader: Reader | None = None, feed: Feed | None = Non
# Set the webhook to read, so we don't send it again. # Set the webhook to read, so we don't send it again.
try: try:
reader.set_entry_read(entry, True) reader.set_entry_read(entry, True)
except EntryNotFoundError as e: except EntryNotFoundError:
logger.error("Error setting entry to read: %s", e) logger.exception("Error setting entry to read: %s", entry.id)
continue continue
except StorageError as e: except StorageError:
logger.error("Error setting entry to read: %s", e) logger.exception("Error setting entry to read: %s", entry.id)
continue continue
# Get the webhook URL for the entry. If it is None, we will continue to the next entry. # Get the webhook URL for the entry. If it is None, we will continue to the next entry.

View File

@ -52,11 +52,13 @@ LOGGING_CONFIG = {
"version": 1, "version": 1,
"disable_existing_loggers": True, "disable_existing_loggers": True,
"formatters": { "formatters": {
"standard": {"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"}, "standard": {
"format": "%(asctime)s [%(processName)s: %(process)d] [%(threadName)s: %(thread)d] [%(levelname)s] %(name)s: %(message)s", # noqa: E501
},
}, },
"handlers": { "handlers": {
"default": { "default": {
"level": "INFO", "level": "DEBUG",
"formatter": "standard", "formatter": "standard",
"class": "logging.StreamHandler", "class": "logging.StreamHandler",
"stream": "ext://sys.stdout", # Default is stderr "stream": "ext://sys.stdout", # Default is stderr
@ -64,7 +66,7 @@ LOGGING_CONFIG = {
}, },
"loggers": { "loggers": {
"": { # root logger "": { # root logger
"level": "INFO", "level": "DEBUG",
"handlers": ["default"], "handlers": ["default"],
"propagate": False, "propagate": False,
}, },

View File

@ -1,7 +1,5 @@
from __future__ import annotations from __future__ import annotations
import logging
import sys
import typing import typing
from functools import lru_cache from functools import lru_cache
from pathlib import Path from pathlib import Path
@ -15,16 +13,6 @@ if typing.TYPE_CHECKING:
data_dir: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True, ensure_exists=True) data_dir: str = user_data_dir(appname="discord_rss_bot", appauthor="TheLovinator", roaming=True, ensure_exists=True)
logger: logging.Logger = logging.getLogger("discord_rss_bot")
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler(sys.stdout)
log_formatter = logging.Formatter(
"%(asctime)s [%(processName)s: %(process)d] [%(threadName)s: %(thread)d] [%(levelname)s] %(name)s: %(message)s",
)
stream_handler.setFormatter(log_formatter)
logger.addHandler(stream_handler)
# TODO(TheLovinator): Add default things to the database and make the edible. # TODO(TheLovinator): Add default things to the database and make the edible.
default_custom_message: JSONType | str = "{{entry_title}}\n{{entry_link}}" default_custom_message: JSONType | str = "{{entry_title}}\n{{entry_link}}"
default_custom_embed: dict[str, str] = { default_custom_embed: dict[str, str] = {

View File

@ -218,7 +218,6 @@
Something went wrong, there was no entry found. If this feed has entries and you still see this message, please contact the developer. Something went wrong, there was no entry found. If this feed has entries and you still see this message, please contact the developer.
{% endif %} {% endif %}
</div> </div>
<label for="title" class="col-sm-6 col-form-label">Title</label> <label for="title" class="col-sm-6 col-form-label">Title</label>
<input name="title" <input name="title"
type="text" type="text"