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