-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the logging by outputting different log levels in different colors. Also, the name `--debug` option is renamed to `--verbose`.
- Loading branch information
Showing
3 changed files
with
51 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import logging | ||
|
||
|
||
class CustomFormatter(logging.Formatter): | ||
LIGHT_CYAN = '\033[0;96m' | ||
GREEN = '\033[0;32m' | ||
YELLOW = '\033[38;5;226m' | ||
RED = '\033[38;5;196m' | ||
BOLD_RED = '\033[31;1m' | ||
RESET = '\033[0m' | ||
|
||
def __init__(self, fmt: str) -> None: | ||
super().__init__() | ||
self.formats = { | ||
logging.DEBUG: self.LIGHT_CYAN + fmt + self.RESET, | ||
logging.INFO: self.GREEN + fmt + self.RESET, | ||
logging.WARNING: self.YELLOW + fmt + self.RESET, | ||
logging.ERROR: self.RED + fmt + self.RESET, | ||
logging.CRITICAL: self.BOLD_RED + fmt + self.RESET | ||
} | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
log_fmt = self.formats.get(record.levelno) | ||
formatter = logging.Formatter(log_fmt) | ||
return formatter.format(record) | ||
|
||
|
||
def setup_logging(debug: bool, color: bool) -> None: | ||
logger = logging.getLogger() | ||
logger.setLevel(logging.NOTSET) | ||
|
||
fmt = '%(levelname)8s | %(message)s' | ||
console_handler = logging.StreamHandler() | ||
console_handler.setLevel( | ||
logging.DEBUG if debug else logging.INFO) | ||
console_handler.setFormatter( | ||
CustomFormatter(fmt) if color else logging.Formatter(fmt)) | ||
|
||
logger.addHandler(console_handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters