Coverage for enderchest/loggers.py: 94%
17 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-03 20:14 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-10-03 20:14 +0000
1"""Loggers for the various EnderChest actions"""
2import logging
4CRAFT_LOGGER = logging.getLogger("enderchest.craft")
5GATHER_LOGGER = logging.getLogger("enderchest.gather")
6PLACE_LOGGER = logging.getLogger("enderchest.place")
7SYNC_LOGGER = logging.getLogger("enderchest.sync")
9IMPORTANT = 25 # INFO logs that should still be displayed on "-q"
10logging.addLevelName(IMPORTANT, "INFO")
13class CLIFormatter(logging.Formatter):
14 """Colorful formatter for the CLI
16 h/t https://stackoverflow.com/a/56944256"""
18 grey = "\x1b[2;20m"
19 yellow = "\x1b[33;20m"
20 bold_red = "\x1b[31;1m"
21 reset = "\x1b[0m"
23 FORMATS = {
24 logging.DEBUG: grey + "%(message)s" + reset,
25 logging.INFO: "%(message)s",
26 IMPORTANT: "%(message)s",
27 logging.WARNING: yellow + "%(message)s" + reset,
28 logging.ERROR: bold_red + "%(message)s" + reset,
29 logging.CRITICAL: bold_red + "%(message)s" + reset,
30 }
32 def format(self, record: logging.LogRecord) -> str:
33 return logging.Formatter(self.FORMATS.get(record.levelno)).format(record)
36def verbosity_to_log_level(verbosity: int) -> int:
37 """Convert a verbosity level (number of `-v`s minus number of `-q`s) to
38 a logging level
40 Parameters
41 ----------
42 verbosity: int
43 A verbosity level usually specified by the number of `-v` flags a user
44 provides minus the number of `-q` flags. As a baseline, a verbosity of
45 0 will set the level to handle all INFO-level messages and above.
47 Returns
48 -------
49 int
50 The corresponding log level that should be set
52 Notes
53 -----
54 Technically the default logging level is set just high enough to exclude
55 DEBUG by default. This allows us to capture intermediate log levels (read:
56 `IMPORTANT`) at the `verbosity = -1` (`-q`) level.
57 """
58 return logging.DEBUG + 1 - 10 * verbosity