Skip to content

Commit

Permalink
feature: use plugin logger as recomended by Mkdocs (road to Mkdocs>=1…
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts authored Dec 16, 2023
2 parents ed2e6fe + c7e4310 commit 36d261a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 72 deletions.
1 change: 1 addition & 0 deletions mkdocs_rss_plugin/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
27 changes: 18 additions & 9 deletions mkdocs_rss_plugin/git_manager/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


# ############################################################################
Expand All @@ -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.info(
"""
[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).
Expand All @@ -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.info(
"""
[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).
Expand All @@ -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.info(
"""
[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/
Expand All @@ -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.info(
"""
[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 \
Expand Down
27 changes: 13 additions & 14 deletions mkdocs_rss_plugin/integrations/theme_material_social_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###############
Expand Down Expand Up @@ -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."
)

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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}."
)

Expand Down
17 changes: 9 additions & 8 deletions mkdocs_rss_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# ##################################

# standard library
import logging
from copy import deepcopy
from datetime import datetime
from email.utils import formatdate
Expand All @@ -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

Expand All @@ -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,
)
Expand All @@ -39,7 +39,8 @@
# ############################################################################
# ########## Globals #############
# ################################
logger = logging.getLogger("mkdocs.mkdocs_rss_plugin")

logger = get_plugin_logger(MKDOCS_LOGGER_NAME)


# ############################################################################
Expand Down Expand Up @@ -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)
Expand All @@ -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"
)
Expand Down
7 changes: 5 additions & 2 deletions mkdocs_rss_plugin/timezoner_pre39.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


# ############################################################################
Expand Down
9 changes: 7 additions & 2 deletions mkdocs_rss_plugin/timezoner_py39.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


# ############################################################################
Expand Down
Loading

0 comments on commit 36d261a

Please sign in to comment.