Coverage for src/mkdocs_manpage/logger.py: 100.00%
12 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-05 18:44 +0200
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-05 18:44 +0200
1"""Logging functions."""
3from __future__ import annotations
5import logging
6from typing import Any, MutableMapping
9class PluginLogger(logging.LoggerAdapter):
10 """A logger adapter to prefix messages with the originating package name."""
12 def __init__(self, prefix: str, logger: logging.Logger):
13 """Initialize the object.
15 Arguments:
16 prefix: The string to insert in front of every message.
17 logger: The logger instance.
18 """
19 super().__init__(logger, {})
20 self.prefix = prefix
22 def process(self, msg: str, kwargs: MutableMapping[str, Any]) -> tuple[str, Any]:
23 """Process the message.
25 Arguments:
26 msg: The message:
27 kwargs: Remaining arguments.
29 Returns:
30 The processed message.
31 """
32 return f"{self.prefix}: {msg}", kwargs
35def get_logger(name: str) -> PluginLogger:
36 """Return a logger for plugins.
38 Arguments:
39 name: The name to use with `logging.getLogger`.
41 Returns:
42 A logger configured to work well in MkDocs,
43 prefixing each message with the plugin package name.
44 """
45 logger = logging.getLogger(f"mkdocs.plugins.{name}")
46 return PluginLogger(name.split(".", 1)[0], logger)