Verified Commit 33ffcebb authored by Sebastian Endres's avatar Sebastian Endres
Browse files

Add colored log formatter: TerminalFormatter

parent f2bb6fdb
"""Utils."""
import argparse
import logging
import os
import sys
import typing
......@@ -8,7 +9,7 @@ from pathlib import Path
from typing import Callable, NamedTuple, Optional, TypeVar, Union
from matplotlib import pyplot as plt
from termcolor import cprint
from termcolor import colored, cprint
from yaspin import yaspin
if typing.TYPE_CHECKING:
......@@ -256,3 +257,28 @@ class Subplot:
def __exit__(self, *args, **kwargs):
plt.close(fig=self.fig)
class TerminalFormatter(logging.Formatter):
"""logging formatter with colors."""
colors = {
logging.DEBUG: "grey",
logging.INFO: "cyan",
logging.WARNING: "yellow",
logging.ERROR: "red",
logging.CRITICAL: "red",
}
attrs = {
logging.CRITICAL: ["bold"],
}
def __init__(self, fmt=f"%(asctime)s | %(message)s"):
super().__init__(fmt, datefmt="%Y-%m-%d %H:%M:%S")
def format(self, record):
return colored(
super().format(record),
color=self.colors[record.levelno],
attrs=self.attrs.get(record.levelno),
)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment