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

1"""Logging functions.""" 

2 

3from __future__ import annotations 

4 

5import logging 

6from typing import Any, MutableMapping 

7 

8 

9class PluginLogger(logging.LoggerAdapter): 

10 """A logger adapter to prefix messages with the originating package name.""" 

11 

12 def __init__(self, prefix: str, logger: logging.Logger): 

13 """Initialize the object. 

14 

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 

21 

22 def process(self, msg: str, kwargs: MutableMapping[str, Any]) -> tuple[str, Any]: 

23 """Process the message. 

24 

25 Arguments: 

26 msg: The message: 

27 kwargs: Remaining arguments. 

28 

29 Returns: 

30 The processed message. 

31 """ 

32 return f"{self.prefix}: {msg}", kwargs 

33 

34 

35def get_logger(name: str) -> PluginLogger: 

36 """Return a logger for plugins. 

37 

38 Arguments: 

39 name: The name to use with `logging.getLogger`. 

40 

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)