Add colours to logging
Signed-off-by: András Schmelczer <andras@schmelczer.dev>
This commit is contained in:
parent
85a06096bf
commit
3730a884b0
1 changed files with 84 additions and 268 deletions
21
good_ai/src/good_ai/utilities/logger/custom_formatter.py
Normal file
21
good_ai/src/good_ai/utilities/logger/custom_formatter.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import logging
|
||||
|
||||
from .colors import BLUE, BOLD_RED, GREY, RED, RESET, YELLOW
|
||||
|
||||
|
||||
class CustomFormatter(logging.Formatter):
|
||||
def __init__(self, fmt: str):
|
||||
super().__init__()
|
||||
self.fmt = fmt
|
||||
self.FORMATS = {
|
||||
logging.DEBUG: GREY + self.fmt + RESET,
|
||||
logging.INFO: BLUE + self.fmt + RESET,
|
||||
logging.WARNING: YELLOW + self.fmt + RESET,
|
||||
logging.ERROR: RED + self.fmt + RESET,
|
||||
logging.CRITICAL: BOLD_RED + self.fmt + RESET,
|
||||
}
|
||||
|
||||
def format(self, record: logging.LogRecord) -> None:
|
||||
log_fmt = self.FORMATS.get(record.levelno)
|
||||
formatter = logging.Formatter(log_fmt)
|
||||
return formatter.format(record)
|
||||
Loading…
Reference in a new issue