diff --git a/mkdocs_rss_plugin/constants.py b/mkdocs_rss_plugin/constants.py index e2f4b7eb..66600d0b 100644 --- a/mkdocs_rss_plugin/constants.py +++ b/mkdocs_rss_plugin/constants.py @@ -16,6 +16,7 @@ DEFAULT_TEMPLATE_FOLDER = Path(__file__).parent / "templates" DEFAULT_TEMPLATE_FILENAME = DEFAULT_TEMPLATE_FOLDER / "rss.xml.jinja2" +MKDOCS_LOGGER_NAME = "[RSS-plugin]" OUTPUT_FEED_CREATED = "feed_rss_created.xml" OUTPUT_FEED_UPDATED = "feed_rss_updated.xml" REMOTE_REQUEST_HEADERS = { diff --git a/mkdocs_rss_plugin/git_manager/ci.py b/mkdocs_rss_plugin/git_manager/ci.py index 646ce326..ab697888 100644 --- a/mkdocs_rss_plugin/git_manager/ci.py +++ b/mkdocs_rss_plugin/git_manager/ci.py @@ -5,11 +5,20 @@ # ################################## # standard library -import logging from os import environ, path # 3rd party from git import Git +from mkdocs.plugins import get_plugin_logger + +# package +from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME + +# ############################################################################ +# ########## Globals ############# +# ################################ + +logger = get_plugin_logger(MKDOCS_LOGGER_NAME) # ############################################################################ @@ -30,9 +39,9 @@ def raise_ci_warnings(self): # Gitlab Runners if environ.get("GITLAB_CI") and n_commits < 50: # Default is GIT_DEPTH of 50 for gitlab - logging.warning( + logger.warning( """ - [rss-plugin] Running on a gitlab runner might lead to wrong \ + Running on a gitlab runner might lead to wrong \ git revision dates due to a shallow git fetch depth. \ Make sure to set GIT_DEPTH to 1000 in your .gitlab-ci.yml file. \ (see https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone). @@ -42,9 +51,9 @@ def raise_ci_warnings(self): # Github Actions if environ.get("GITHUB_ACTIONS") and n_commits == 1: # Default is fetch-depth of 1 for github actions - logging.warning( + logger.warning( """ - [rss-plugin] Running on github actions might lead to wrong \ + Running on github actions might lead to wrong \ git revision dates due to a shallow git fetch depth. \ Try setting fetch-depth to 0 in your github action \ (see https://github.com/actions/checkout). @@ -54,9 +63,9 @@ def raise_ci_warnings(self): # Bitbucket pipelines if environ.get("CI") and n_commits < 50: # Default is fetch-depth of 50 for bitbucket pipelines - logging.warning( + logger.warning( """ - [rss-plugin] Running on bitbucket pipelines might lead to wrong \ + Running on bitbucket pipelines might lead to wrong \ git revision dates due to a shallow git fetch depth. \ Try setting "clone: depth" to "full" in your pipeline \ (see https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/ @@ -67,9 +76,9 @@ def raise_ci_warnings(self): # Azure Devops Pipeline # Does not limit fetch-depth by default if environ.get("Agent.Source.Git.ShallowFetchDepth", 10e99) < n_commits: - logging.warning( + logger.warning( """ - [rss-plugin] Running on Azure pipelines \ + Running on Azure pipelines \ with limited fetch-depth might lead to wrong git revision dates \ due to a shallow git fetch depth. \ Remove any Shallow Fetch settings \ diff --git a/mkdocs_rss_plugin/integrations/theme_material_social_plugin.py b/mkdocs_rss_plugin/integrations/theme_material_social_plugin.py index 70808736..a8f6e42e 100644 --- a/mkdocs_rss_plugin/integrations/theme_material_social_plugin.py +++ b/mkdocs_rss_plugin/integrations/theme_material_social_plugin.py @@ -5,19 +5,22 @@ # ################################## # standard library -import logging from pathlib import Path from typing import Optional # 3rd party from mkdocs.config.config_options import Config +from mkdocs.plugins import get_plugin_logger from mkdocs.structure.pages import Page +# package +from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME + # ############################################################################ # ########## Globals ############# # ################################ -logger = logging.getLogger("mkdocs.mkdocs_rss_plugin") +logger = get_plugin_logger(MKDOCS_LOGGER_NAME) # ############################################################################ # ########## Logic ############### @@ -58,7 +61,7 @@ def __init__(self, mkdocs_config: Config, switch_force: bool = True) -> None: if switch_force is False: self.IS_ENABLED = False logger.debug( - "[rss-plugin] Integration with Social Cards (Material theme) is " + "Integration with Social Cards (Material theme) is " "disabled in plugin's option in Mkdocs configuration." ) @@ -92,23 +95,21 @@ def is_social_plugin_enabled_mkdocs(self, mkdocs_config: Config) -> bool: bool: True if the theme material and the plugin social cards is enabled. """ if not self.is_theme_material(mkdocs_config=mkdocs_config): - logger.debug( - "[rss-plugin] Installed theme is not 'material'. Integration disabled." - ) + logger.debug("Installed theme is not 'material'. Integration disabled.") return False if not mkdocs_config.plugins.get("material/social"): - logger.debug("[rss-plugin] Social plugin not listed in configuration.") + logger.debug("Social plugin not listed in configuration.") return False social_plugin_cfg = mkdocs_config.plugins.get("material/social") if not social_plugin_cfg.config.enabled: - logger.debug("[rss-plugin] Social plugin is installed but disabled.") + logger.debug("Social plugin is installed but disabled.") self.IS_SOCIAL_PLUGIN_ENABLED = False return False - logger.debug("[rss-plugin] Social plugin is enabled in Mkdocs configuration.") + logger.debug("Social plugin is enabled in Mkdocs configuration.") self.IS_SOCIAL_PLUGIN_CARDS_ENABLED = True return True @@ -127,13 +128,11 @@ def is_social_plugin_and_cards_enabled_mkdocs(self, mkdocs_config: Config) -> bo social_plugin_cfg = mkdocs_config.plugins.get("material/social") if not social_plugin_cfg.config.cards: - logger.debug( - "[rss-plugin] Social plugin is installed, present but cards are disabled." - ) + logger.debug("Social plugin is installed, present but cards are disabled.") self.IS_SOCIAL_PLUGIN_CARDS_ENABLED = False return False - logger.debug("[rss-plugin] Social cards are enabled in Mkdocs configuration.") + logger.debug("Social cards are enabled in Mkdocs configuration.") self.IS_SOCIAL_PLUGIN_CARDS_ENABLED = True return True @@ -168,7 +167,7 @@ def get_social_cards_dir(self, mkdocs_config: Config) -> str: social_plugin_cfg = mkdocs_config.plugins.get("material/social") logger.debug( - "[rss-plugin] Social cards folder in Mkdocs build directory: " + "Social cards folder in Mkdocs build directory: " f"{social_plugin_cfg.config.cards_dir}." ) diff --git a/mkdocs_rss_plugin/plugin.py b/mkdocs_rss_plugin/plugin.py index be71a16c..9e2af8c0 100644 --- a/mkdocs_rss_plugin/plugin.py +++ b/mkdocs_rss_plugin/plugin.py @@ -5,7 +5,6 @@ # ################################## # standard library -import logging from copy import deepcopy from datetime import datetime from email.utils import formatdate @@ -17,7 +16,7 @@ from jinja2 import Environment, FileSystemLoader, select_autoescape from mkdocs.config import config_options from mkdocs.exceptions import PluginError -from mkdocs.plugins import BasePlugin, event_priority +from mkdocs.plugins import BasePlugin, event_priority, get_plugin_logger from mkdocs.structure.pages import Page from mkdocs.utils import get_build_timestamp @@ -27,6 +26,7 @@ from mkdocs_rss_plugin.constants import ( DEFAULT_TEMPLATE_FILENAME, DEFAULT_TEMPLATE_FOLDER, + MKDOCS_LOGGER_NAME, OUTPUT_FEED_CREATED, OUTPUT_FEED_UPDATED, ) @@ -39,7 +39,8 @@ # ############################################################################ # ########## Globals ############# # ################################ -logger = logging.getLogger("mkdocs.mkdocs_rss_plugin") + +logger = get_plugin_logger(MKDOCS_LOGGER_NAME) # ############################################################################ @@ -143,24 +144,24 @@ def on_config(self, config: config_options.Config) -> dict: ) except ValueError as err: raise PluginError( - "[rss-plugin] Config error: `date_from_meta.default_time` value " + "Config error: `date_from_meta.default_time` value " f"'{self.meta_default_time}' format doesn't match the expected " f"format %H:%M. Trace: {err}" ) if self.config.use_git: logger.debug( - "[rss-plugin] Dates will be retrieved FIRSTLY from page meta (yaml " + "Dates will be retrieved FIRSTLY from page meta (yaml " "frontmatter). The git log will be used as fallback." ) else: logger.debug( - "[rss-plugin] Dates will be retrieved ONLY from page meta (yaml " + "Dates will be retrieved ONLY from page meta (yaml " "frontmatter). The build date will be used as fallback, without any " "call to Git." ) else: - logger.debug("[rss-plugin] Dates will be retrieved from git log.") + logger.debug("Dates will be retrieved from git log.") # create 2 final dicts self.feed_created = deepcopy(base_feed) @@ -177,7 +178,7 @@ def on_config(self, config: config_options.Config) -> dict: ) else: logger.error( - "[rss-plugin] The variable `site_url` is not set in the MkDocs " + "The variable `site_url` is not set in the MkDocs " "configuration file whereas a URL is mandatory to publish. " "See: https://validator.w3.org/feed/docs/rss2.html#requiredChannelElements" ) diff --git a/mkdocs_rss_plugin/timezoner_pre39.py b/mkdocs_rss_plugin/timezoner_pre39.py index d969eeb0..73ae7e5b 100644 --- a/mkdocs_rss_plugin/timezoner_pre39.py +++ b/mkdocs_rss_plugin/timezoner_pre39.py @@ -11,18 +11,21 @@ # ################################## # standard library -import logging from datetime import datetime # 3rd party import pytz +from mkdocs.plugins import get_plugin_logger + +# package +from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME # ############################################################################ # ########## Globals ############# # ################################ -logger = logging.getLogger("mkdocs.mkdocs_rss_plugin") +logger = get_plugin_logger(MKDOCS_LOGGER_NAME) # ############################################################################ diff --git a/mkdocs_rss_plugin/timezoner_py39.py b/mkdocs_rss_plugin/timezoner_py39.py index 034ecb7b..e2108abc 100644 --- a/mkdocs_rss_plugin/timezoner_py39.py +++ b/mkdocs_rss_plugin/timezoner_py39.py @@ -11,16 +11,21 @@ # ################################## # standard library -import logging from datetime import datetime, timezone from zoneinfo import ZoneInfo +# 3rd party +from mkdocs.plugins import get_plugin_logger + +# package +from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME + # ############################################################################ # ########## Globals ############# # ################################ -logger = logging.getLogger("mkdocs.mkdocs_rss_plugin") +logger = get_plugin_logger(MKDOCS_LOGGER_NAME) # ############################################################################ diff --git a/mkdocs_rss_plugin/util.py b/mkdocs_rss_plugin/util.py index 6f6be0b8..21b19e96 100644 --- a/mkdocs_rss_plugin/util.py +++ b/mkdocs_rss_plugin/util.py @@ -21,11 +21,12 @@ import markdown from git import GitCommandError, GitCommandNotFound, InvalidGitRepositoryError, Repo from mkdocs.config.config_options import Config +from mkdocs.plugins import get_plugin_logger from mkdocs.structure.pages import Page from mkdocs.utils import get_build_datetime # package -from mkdocs_rss_plugin.constants import REMOTE_REQUEST_HEADERS +from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME, REMOTE_REQUEST_HEADERS from mkdocs_rss_plugin.git_manager.ci import CiHandler from mkdocs_rss_plugin.integrations.theme_material_social_plugin import ( IntegrationMaterialSocialCards, @@ -41,8 +42,7 @@ # ########## Globals ############# # ################################ -logger = logging.getLogger("mkdocs.mkdocs_rss_plugin") - +logger = get_plugin_logger(MKDOCS_LOGGER_NAME) # ############################################################################ # ########## Classes ############# @@ -71,14 +71,14 @@ def __init__( integration with Social Cards plugin from Material theme. Defaults to True. """ if use_git: - logger.debug("[rss-plugin] Git use is disabled.") + logger.debug("Git use is enabled.") try: git_repo = Repo(path, search_parent_directories=True) self.repo = git_repo.git self.git_is_valid = True except InvalidGitRepositoryError as err: logger.warning( - f"[rss-plugin] Path '{path}' is not a valid git directory. " + f"Path '{path}' is not a valid git directory. " "Only page.meta (YAML frontmatter will be used). " "To disable this warning, set 'use_git: false' in plugin options. " f"Trace: {err}" @@ -87,7 +87,7 @@ def __init__( use_git = False except Exception as err: logger.warning( - f"[rss-plugin] Unrecognized git issue. " + f"Unrecognized git issue. " "Only page.meta (YAML frontmatter will be used). " "To disable this warning, set 'use_git: false' in plugin options. " f"Trace: {err}" @@ -101,7 +101,7 @@ def __init__( else: self.git_is_valid = False logger.debug( - "[rss-plugin] Git use is disabled. " + "Git use is disabled. " "Only page.meta (YAML frontmatter will be used). " ) @@ -127,7 +127,7 @@ def build_url(self, base_url: str, path: str, args_dict: dict = None) -> str: """ if not base_url: logger.error( - "[rss-plugin] Base url not set, probably because 'site_url' is not set " + "Base url not set, probably because 'site_url' is not set " "in Mkdocs configuration file. Using an empty string instead." ) base_url = "" @@ -184,13 +184,13 @@ def get_file_dates( ) if isinstance(dt_created, str): logger.info( - f"[rss-plugin] Creation date of {in_page.file.abs_src_path} is an " + f"Creation date of {in_page.file.abs_src_path} is an " f"a character string: {dt_created} ({type(dt_created)})" ) elif dt_created is None: logger.info( - f"[rss-plugin] Creation date of {in_page.file.abs_src_path} has not " + f"Creation date of {in_page.file.abs_src_path} has not " "been recognized." ) @@ -206,13 +206,13 @@ def get_file_dates( if isinstance(dt_updated, str): logger.info( - f"[rss-plugin] Update date of {in_page.file.abs_src_path} is an " + f"Update date of {in_page.file.abs_src_path} is an " f"a character string: {dt_updated} ({type(dt_updated)})" ) elif dt_updated is None: logger.info( - f"[rss-plugin] Update date of {in_page.file.abs_src_path} is an " + f"Update date of {in_page.file.abs_src_path} is an " f"unrecognized type: {dt_updated} ({type(dt_updated)})" ) @@ -237,14 +237,14 @@ def get_file_dates( ) except GitCommandError as err: logging.warning( - f"[rss-plugin] Unable to read git logs of '{in_page.file.abs_src_path}'. " + f"Unable to read git logs of '{in_page.file.abs_src_path}'. " "Is git log readable? Falling back to build date. " "To disable this warning, set 'use_git: false' in plugin options. " f"Trace: {err}" ) except GitCommandNotFound as err: logging.error( - "[rss-plugin] Unable to perform command 'git log'. Is git installed? " + "Unable to perform command 'git log'. Is git installed? " "Falling back to build date. " "To disable this warning, set 'use_git: false' in plugin options. " f"Trace: {err}" @@ -270,7 +270,7 @@ def get_file_dates( ) elif dt_created: logger.info( - f"[rss-plugin] Updated date could not be retrieved for page: " + f"Updated date could not be retrieved for page: " f"{in_page.file.abs_src_path}. Maybe it has never been committed yet?" ) return ( @@ -279,7 +279,7 @@ def get_file_dates( ) elif dt_updated: logger.info( - f"[rss-plugin] Creation date could not be retrieved for page: " + f"Creation date could not be retrieved for page: " f"{in_page.file.abs_src_path}. Maybe it has never been committed yet?" ) return ( @@ -288,7 +288,7 @@ def get_file_dates( ) else: logging.warning( - f"[rss-plugin] Dates could not be retrieved for page: {in_page.file.abs_src_path}." + f"Dates could not be retrieved for page: {in_page.file.abs_src_path}." ) return ( get_build_datetime(), @@ -313,7 +313,7 @@ def get_authors_from_meta(self, in_page: Page) -> Tuple[str] or None: return tuple(in_page.meta.get("author")) else: logging.warning( - "[rss-plugin] Type of author value in page.meta " + "Type of author value in page.meta " f"({in_page.file.abs_src_path}) is not valid. " "It should be str, list or tuple, " f"not: {type(in_page.meta.get('author'))}." @@ -326,7 +326,7 @@ def get_authors_from_meta(self, in_page: Page) -> Tuple[str] or None: return tuple(in_page.meta.get("authors")) else: logging.warning( - "[rss-plugin] Type of authors value in page.meta (%s) is not valid. " + "Type of authors value in page.meta (%s) is not valid. " "It should be str, list or tuple, not: %s." % in_page.file.abs_src_path, type(in_page.meta.get("authors")), @@ -395,19 +395,17 @@ def get_date_from_meta( time_to_add = datetime.min.time() out_date = datetime.combine(date_metatag_value, time_to_add) else: - logger.debug( - f"[rss-plugin] Incompatible date type: {type(date_metatag_value)}" - ) + logger.debug(f"Incompatible date type: {type(date_metatag_value)}") return out_date except ValueError as err: logger.error( - f"[rss-plugin] Incompatible date found: {date_metatag_value=} " + f"Incompatible date found: {date_metatag_value=} " f"{type(date_metatag_value)}. Trace: {err}" ) return out_date except Exception as err: logger.error( - f"[rss-plugin] Unable to retrieve creation date: {date_metatag_value=} " + f"Unable to retrieve creation date: {date_metatag_value=} " f"{type(date_metatag_value)}. Trace: {err}" ) return out_date @@ -445,7 +443,7 @@ def get_description_or_abstract( # If no description and chars_count set to 0, return empty string elif not description and chars_count == 0: logger.warning( - f"[rss-plugin] No description set for page {in_page.file.src_uri} " + f"No description set for page {in_page.file.src_uri} " "and 'abstract_chars_count' set to 0. The feed won't be compliant, " "because an item must have a description." ) @@ -495,13 +493,13 @@ def get_image(self, in_page: Page, base_url: str) -> Optional[Tuple[str, str, in if in_page.meta.get("image"): img_url = in_page.meta.get("image").strip() logger.debug( - f"[rss-plugin] Image found ({img_url}) in page.meta.image for page: " + f"Image found ({img_url}) in page.meta.image for page: " f"{in_page.file.src_uri}" ) elif in_page.meta.get("illustration"): img_url = in_page.meta.get("illustration").strip() logger.debug( - f"[rss-plugin] Image found ({img_url}) in page.meta.illustration for page: " + f"Image found ({img_url}) in page.meta.illustration for page: " f"{in_page.file.src_uri}" ) elif ( @@ -519,7 +517,7 @@ def get_image(self, in_page: Page, base_url: str) -> Optional[Tuple[str, str, in mkdocs_page=in_page ) logger.debug( - f"[rss-plugin] Image found ({img_url}) from social cards for page: " + f"Image found ({img_url}) from social cards for page: " f"{in_page.file.src_uri}. Using local image to get mime and length: " f"{img_local_path}" ) @@ -527,9 +525,7 @@ def get_image(self, in_page: Page, base_url: str) -> Optional[Tuple[str, str, in if img_local_path.is_file(): img_length = img_local_path.stat().st_size else: - logger.debug( - f"[rss-plugin] Social card: {img_local_path} still not exists." - ) + logger.debug(f"Social card: {img_local_path} still not exists.") img_length = None return ( @@ -610,7 +606,7 @@ def get_remote_image_length( img_length = remote_img.getheader("content-length") except (HTTPError, URLError) as err: logging.warning( - f"[rss-plugin] Remote image could not been reached: {image_url}. " + f"Remote image could not been reached: {image_url}. " f"Trying again with GET and disabling SSL verification. Attempt: {attempt}. " f"Trace: {err}" ) @@ -623,7 +619,7 @@ def get_remote_image_length( ) else: logging.error( - f"[rss-plugin] Remote image is not reachable: {image_url} after " + f"Remote image is not reachable: {image_url} after " f"{attempt} attempts. Trace: {err}" ) return None @@ -671,7 +667,7 @@ def guess_locale(self, mkdocs_config: Config) -> str or None: if mkdocs_config.get("locale"): logger.warning( DeprecationWarning( - "[rss-plugin] Mkdocs does not support locale option at the " + "Mkdocs does not support locale option at the " "configuration root but under theme sub-configuration. It won't be " "supported anymore by the plugin in the next version." ) @@ -687,7 +683,7 @@ def guess_locale(self, mkdocs_config: Config) -> str or None: # TODO: remove custom behavior when Material theme switches to locale # see: https://github.com/squidfunk/mkdocs-material/discussions/6453 logger.debug( - "[rss plugin] Language detected in Material theme " + "Language detected in Material theme " f"('{mkdocs_config.theme.name}') settings: " f"{mkdocs_config.theme.get('language')}" ) @@ -696,7 +692,7 @@ def guess_locale(self, mkdocs_config: Config) -> str or None: elif "locale" in mkdocs_config.theme: locale = mkdocs_config.theme.locale logger.debug( - "[rss plugin] Locale detected in theme " + "Locale detected in theme " f"('{mkdocs_config.theme.name}') settings: {locale=}" ) return ( @@ -706,7 +702,7 @@ def guess_locale(self, mkdocs_config: Config) -> str or None: ) else: logger.debug( - "[rss plugin] Nor locale or language detected in theme settings " + "Nor locale or language detected in theme settings " f"('{mkdocs_config.theme.name}')." )