Coverage for enderchest/loggers.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-04 01:41 +0000

1"""Loggers for the various EnderChest actions""" 

2 

3import logging 

4 

5BREAK_LOGGER = logging.getLogger("enderchest.break") 

6CRAFT_LOGGER = logging.getLogger("enderchest.craft") 

7GATHER_LOGGER = logging.getLogger("enderchest.gather") 

8INVENTORY_LOGGER = logging.getLogger("enderchest.inventory") 

9PLACE_LOGGER = logging.getLogger("enderchest.place") 

10SYNC_LOGGER = logging.getLogger("enderchest.sync") 

11 

12IMPORTANT = 25 # INFO logs that should still be displayed on "-q" 

13logging.addLevelName(IMPORTANT, "INFO") 

14 

15 

16class CLIFormatter(logging.Formatter): 

17 """Colorful formatter for the CLI 

18 

19 h/t https://stackoverflow.com/a/56944256""" 

20 

21 grey = "\x1b[2;20m" 

22 yellow = "\x1b[33;20m" 

23 bold_red = "\x1b[31;1m" 

24 reset = "\x1b[0m" 

25 

26 FORMATS = { 

27 logging.DEBUG: grey + "%(message)s" + reset, 

28 logging.INFO: "%(message)s", 

29 IMPORTANT: "%(message)s", 

30 logging.WARNING: yellow + "%(message)s" + reset, 

31 logging.ERROR: bold_red + "%(message)s" + reset, 

32 logging.CRITICAL: bold_red + "%(message)s" + reset, 

33 } 

34 

35 def format(self, record: logging.LogRecord) -> str: 

36 return logging.Formatter(self.FORMATS.get(record.levelno)).format(record) 

37 

38 

39def verbosity_to_log_level(verbosity: int) -> int: 

40 """Convert a verbosity level (number of `-v`s minus number of `-q`s) to 

41 a logging level 

42 

43 Parameters 

44 ---------- 

45 verbosity: int 

46 A verbosity level usually specified by the number of `-v` flags a user 

47 provides minus the number of `-q` flags. As a baseline, a verbosity of 

48 0 will set the level to handle all INFO-level messages and above. 

49 

50 Returns 

51 ------- 

52 int 

53 The corresponding log level that should be set 

54 

55 Notes 

56 ----- 

57 Technically the default logging level is set just high enough to exclude 

58 DEBUG by default. This allows us to capture intermediate log levels (read: 

59 `IMPORTANT`) at the `verbosity = -1` (`-q`) level. 

60 """ 

61 return logging.DEBUG + 1 - 10 * verbosity