Skip to content

Commit 36d261a

Browse files
authored
feature: use plugin logger as recomended by Mkdocs (road to Mkdocs>=1.4) (#221)
See: https://www.mkdocs.org/dev-guide/plugins/#logging-in-plugins
2 parents ed2e6fe + c7e4310 commit 36d261a

7 files changed

Lines changed: 86 additions & 72 deletions

File tree

mkdocs_rss_plugin/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
DEFAULT_TEMPLATE_FOLDER = Path(__file__).parent / "templates"
1818
DEFAULT_TEMPLATE_FILENAME = DEFAULT_TEMPLATE_FOLDER / "rss.xml.jinja2"
19+
MKDOCS_LOGGER_NAME = "[RSS-plugin]"
1920
OUTPUT_FEED_CREATED = "feed_rss_created.xml"
2021
OUTPUT_FEED_UPDATED = "feed_rss_updated.xml"
2122
REMOTE_REQUEST_HEADERS = {

mkdocs_rss_plugin/git_manager/ci.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55
# ##################################
66

77
# standard library
8-
import logging
98
from os import environ, path
109

1110
# 3rd party
1211
from git import Git
12+
from mkdocs.plugins import get_plugin_logger
13+
14+
# package
15+
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME
16+
17+
# ############################################################################
18+
# ########## Globals #############
19+
# ################################
20+
21+
logger = get_plugin_logger(MKDOCS_LOGGER_NAME)
1322

1423

1524
# ############################################################################
@@ -30,9 +39,9 @@ def raise_ci_warnings(self):
3039
# Gitlab Runners
3140
if environ.get("GITLAB_CI") and n_commits < 50:
3241
# Default is GIT_DEPTH of 50 for gitlab
33-
logging.warning(
42+
logger.info(
3443
"""
35-
[rss-plugin] Running on a gitlab runner might lead to wrong \
44+
Running on a gitlab runner might lead to wrong \
3645
git revision dates due to a shallow git fetch depth. \
3746
Make sure to set GIT_DEPTH to 1000 in your .gitlab-ci.yml file. \
3847
(see https://docs.gitlab.com/ee/user/project/pipelines/settings.html#git-shallow-clone).
@@ -42,9 +51,9 @@ def raise_ci_warnings(self):
4251
# Github Actions
4352
if environ.get("GITHUB_ACTIONS") and n_commits == 1:
4453
# Default is fetch-depth of 1 for github actions
45-
logging.warning(
54+
logger.info(
4655
"""
47-
[rss-plugin] Running on github actions might lead to wrong \
56+
Running on github actions might lead to wrong \
4857
git revision dates due to a shallow git fetch depth. \
4958
Try setting fetch-depth to 0 in your github action \
5059
(see https://github.com/actions/checkout).
@@ -54,9 +63,9 @@ def raise_ci_warnings(self):
5463
# Bitbucket pipelines
5564
if environ.get("CI") and n_commits < 50:
5665
# Default is fetch-depth of 50 for bitbucket pipelines
57-
logging.warning(
66+
logger.info(
5867
"""
59-
[rss-plugin] Running on bitbucket pipelines might lead to wrong \
68+
Running on bitbucket pipelines might lead to wrong \
6069
git revision dates due to a shallow git fetch depth. \
6170
Try setting "clone: depth" to "full" in your pipeline \
6271
(see https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/
@@ -67,9 +76,9 @@ def raise_ci_warnings(self):
6776
# Azure Devops Pipeline
6877
# Does not limit fetch-depth by default
6978
if environ.get("Agent.Source.Git.ShallowFetchDepth", 10e99) < n_commits:
70-
logging.warning(
79+
logger.info(
7180
"""
72-
[rss-plugin] Running on Azure pipelines \
81+
Running on Azure pipelines \
7382
with limited fetch-depth might lead to wrong git revision dates \
7483
due to a shallow git fetch depth. \
7584
Remove any Shallow Fetch settings \

mkdocs_rss_plugin/integrations/theme_material_social_plugin.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55
# ##################################
66

77
# standard library
8-
import logging
98
from pathlib import Path
109
from typing import Optional
1110

1211
# 3rd party
1312
from mkdocs.config.config_options import Config
13+
from mkdocs.plugins import get_plugin_logger
1414
from mkdocs.structure.pages import Page
1515

16+
# package
17+
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME
18+
1619
# ############################################################################
1720
# ########## Globals #############
1821
# ################################
1922

20-
logger = logging.getLogger("mkdocs.mkdocs_rss_plugin")
23+
logger = get_plugin_logger(MKDOCS_LOGGER_NAME)
2124

2225
# ############################################################################
2326
# ########## Logic ###############
@@ -58,7 +61,7 @@ def __init__(self, mkdocs_config: Config, switch_force: bool = True) -> None:
5861
if switch_force is False:
5962
self.IS_ENABLED = False
6063
logger.debug(
61-
"[rss-plugin] Integration with Social Cards (Material theme) is "
64+
"Integration with Social Cards (Material theme) is "
6265
"disabled in plugin's option in Mkdocs configuration."
6366
)
6467

@@ -92,23 +95,21 @@ def is_social_plugin_enabled_mkdocs(self, mkdocs_config: Config) -> bool:
9295
bool: True if the theme material and the plugin social cards is enabled.
9396
"""
9497
if not self.is_theme_material(mkdocs_config=mkdocs_config):
95-
logger.debug(
96-
"[rss-plugin] Installed theme is not 'material'. Integration disabled."
97-
)
98+
logger.debug("Installed theme is not 'material'. Integration disabled.")
9899
return False
99100

100101
if not mkdocs_config.plugins.get("material/social"):
101-
logger.debug("[rss-plugin] Social plugin not listed in configuration.")
102+
logger.debug("Social plugin not listed in configuration.")
102103
return False
103104

104105
social_plugin_cfg = mkdocs_config.plugins.get("material/social")
105106

106107
if not social_plugin_cfg.config.enabled:
107-
logger.debug("[rss-plugin] Social plugin is installed but disabled.")
108+
logger.debug("Social plugin is installed but disabled.")
108109
self.IS_SOCIAL_PLUGIN_ENABLED = False
109110
return False
110111

111-
logger.debug("[rss-plugin] Social plugin is enabled in Mkdocs configuration.")
112+
logger.debug("Social plugin is enabled in Mkdocs configuration.")
112113
self.IS_SOCIAL_PLUGIN_CARDS_ENABLED = True
113114
return True
114115

@@ -127,13 +128,11 @@ def is_social_plugin_and_cards_enabled_mkdocs(self, mkdocs_config: Config) -> bo
127128
social_plugin_cfg = mkdocs_config.plugins.get("material/social")
128129

129130
if not social_plugin_cfg.config.cards:
130-
logger.debug(
131-
"[rss-plugin] Social plugin is installed, present but cards are disabled."
132-
)
131+
logger.debug("Social plugin is installed, present but cards are disabled.")
133132
self.IS_SOCIAL_PLUGIN_CARDS_ENABLED = False
134133
return False
135134

136-
logger.debug("[rss-plugin] Social cards are enabled in Mkdocs configuration.")
135+
logger.debug("Social cards are enabled in Mkdocs configuration.")
137136
self.IS_SOCIAL_PLUGIN_CARDS_ENABLED = True
138137
return True
139138

@@ -168,7 +167,7 @@ def get_social_cards_dir(self, mkdocs_config: Config) -> str:
168167
social_plugin_cfg = mkdocs_config.plugins.get("material/social")
169168

170169
logger.debug(
171-
"[rss-plugin] Social cards folder in Mkdocs build directory: "
170+
"Social cards folder in Mkdocs build directory: "
172171
f"{social_plugin_cfg.config.cards_dir}."
173172
)
174173

mkdocs_rss_plugin/plugin.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# ##################################
66

77
# standard library
8-
import logging
98
from copy import deepcopy
109
from datetime import datetime
1110
from email.utils import formatdate
@@ -17,7 +16,7 @@
1716
from jinja2 import Environment, FileSystemLoader, select_autoescape
1817
from mkdocs.config import config_options
1918
from mkdocs.exceptions import PluginError
20-
from mkdocs.plugins import BasePlugin, event_priority
19+
from mkdocs.plugins import BasePlugin, event_priority, get_plugin_logger
2120
from mkdocs.structure.pages import Page
2221
from mkdocs.utils import get_build_timestamp
2322

@@ -27,6 +26,7 @@
2726
from mkdocs_rss_plugin.constants import (
2827
DEFAULT_TEMPLATE_FILENAME,
2928
DEFAULT_TEMPLATE_FOLDER,
29+
MKDOCS_LOGGER_NAME,
3030
OUTPUT_FEED_CREATED,
3131
OUTPUT_FEED_UPDATED,
3232
)
@@ -39,7 +39,8 @@
3939
# ############################################################################
4040
# ########## Globals #############
4141
# ################################
42-
logger = logging.getLogger("mkdocs.mkdocs_rss_plugin")
42+
43+
logger = get_plugin_logger(MKDOCS_LOGGER_NAME)
4344

4445

4546
# ############################################################################
@@ -143,24 +144,24 @@ def on_config(self, config: config_options.Config) -> dict:
143144
)
144145
except ValueError as err:
145146
raise PluginError(
146-
"[rss-plugin] Config error: `date_from_meta.default_time` value "
147+
"Config error: `date_from_meta.default_time` value "
147148
f"'{self.meta_default_time}' format doesn't match the expected "
148149
f"format %H:%M. Trace: {err}"
149150
)
150151

151152
if self.config.use_git:
152153
logger.debug(
153-
"[rss-plugin] Dates will be retrieved FIRSTLY from page meta (yaml "
154+
"Dates will be retrieved FIRSTLY from page meta (yaml "
154155
"frontmatter). The git log will be used as fallback."
155156
)
156157
else:
157158
logger.debug(
158-
"[rss-plugin] Dates will be retrieved ONLY from page meta (yaml "
159+
"Dates will be retrieved ONLY from page meta (yaml "
159160
"frontmatter). The build date will be used as fallback, without any "
160161
"call to Git."
161162
)
162163
else:
163-
logger.debug("[rss-plugin] Dates will be retrieved from git log.")
164+
logger.debug("Dates will be retrieved from git log.")
164165

165166
# create 2 final dicts
166167
self.feed_created = deepcopy(base_feed)
@@ -177,7 +178,7 @@ def on_config(self, config: config_options.Config) -> dict:
177178
)
178179
else:
179180
logger.error(
180-
"[rss-plugin] The variable `site_url` is not set in the MkDocs "
181+
"The variable `site_url` is not set in the MkDocs "
181182
"configuration file whereas a URL is mandatory to publish. "
182183
"See: https://validator.w3.org/feed/docs/rss2.html#requiredChannelElements"
183184
)

mkdocs_rss_plugin/timezoner_pre39.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@
1111
# ##################################
1212

1313
# standard library
14-
import logging
1514
from datetime import datetime
1615

1716
# 3rd party
1817
import pytz
18+
from mkdocs.plugins import get_plugin_logger
19+
20+
# package
21+
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME
1922

2023
# ############################################################################
2124
# ########## Globals #############
2225
# ################################
2326

2427

25-
logger = logging.getLogger("mkdocs.mkdocs_rss_plugin")
28+
logger = get_plugin_logger(MKDOCS_LOGGER_NAME)
2629

2730

2831
# ############################################################################

mkdocs_rss_plugin/timezoner_py39.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@
1111
# ##################################
1212

1313
# standard library
14-
import logging
1514
from datetime import datetime, timezone
1615
from zoneinfo import ZoneInfo
1716

17+
# 3rd party
18+
from mkdocs.plugins import get_plugin_logger
19+
20+
# package
21+
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME
22+
1823
# ############################################################################
1924
# ########## Globals #############
2025
# ################################
2126

2227

23-
logger = logging.getLogger("mkdocs.mkdocs_rss_plugin")
28+
logger = get_plugin_logger(MKDOCS_LOGGER_NAME)
2429

2530

2631
# ############################################################################

0 commit comments

Comments
 (0)