-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogging.py
More file actions
51 lines (39 loc) · 1.29 KB
/
Copy pathlogging.py
File metadata and controls
51 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Logging utilities."""
import logging
from typing import Any, MutableMapping, Tuple
class AutoApiLogger(logging.LoggerAdapter):
"""A logger adapter to prefix messages with the originating package name."""
def __init__(self, prefix: str, logger: logging.Logger):
"""Initialize the object.
Arguments:
prefix: The string to insert in front of every message.
logger: The logger instance.
"""
super().__init__(logger, {})
self.prefix = prefix
def process(
self, msg: str, kwargs: MutableMapping[str, Any]
) -> Tuple[str, Any]:
"""Process the message.
Args:
msg:
The message.
kwargs:
Remaining arguments.
Returns:
The processed message.
"""
return f"{self.prefix}: {msg}", kwargs
def get_logger(name: str) -> AutoApiLogger:
"""Return a logger for plugins.
Arguments:
name: The name to use with `logging.getLogger`.
Returns:
A logger configured to work well in MkDocs,
prefixing each message with the plugin package name.
"""
logger = logging.getLogger(f"mkdocs.plugins.{name}")
return AutoApiLogger(
prefix=name.split(".", 1)[0],
logger=logger,
)