Skip to content

Commit 4c8f918

Browse files
author
Matthew Brandt
committed
Add hook to startup event which suprresses certain log messages from autorefs.
1 parent e3d006d commit 4c8f918

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/src/hooks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# # Note: Edits to hooks do not get captured by MkDocs' built-in reloading mechanism.
33
# If you change the hooks, you must manually stop and restart the server.
44
import copy
5+
import logging
56
import re
67
from pathlib import Path
78
from typing import List, Optional, TypedDict
@@ -12,6 +13,31 @@
1213
from parsers.cnrecs import Field, FieldGroup, Record, RecordsParser
1314
from slugify import slugify
1415

16+
root_logger = logging.getLogger("mkdocs")
17+
18+
19+
def on_startup(command, dirty):
20+
class SuppressAutorefsWarnings(logging.Filter):
21+
def filter(self, record):
22+
message = record.getMessage()
23+
should_suppress = record.levelno == logging.WARNING and (
24+
message.startswith("mkdocs_autorefs: Multiple primary URLs found for")
25+
or message.startswith("mkdocs_autorefs: Could not find closest primary URL")
26+
)
27+
return not should_suppress
28+
29+
# Autorefs uses several loggers, including _internal ones. Since I couldn't figure
30+
# out which logger was responsible for issuing the warnings we want to suppress,
31+
# simply attach the filter to all loggers that are part of the plugin.
32+
autorefs_loggers = [
33+
logging.getLogger(path)
34+
for path in list(logging.root.manager.loggerDict.keys())
35+
if path.startswith("mkdocs.plugins.mkdocs_autorefs")
36+
]
37+
38+
for logger in autorefs_loggers:
39+
logger.addFilter(SuppressAutorefsWarnings())
40+
1541

1642
class RecordPrefixClass(TypedDict, total=False):
1743
value: str

0 commit comments

Comments
 (0)