Source code for aiomisc_log.formatter.color

import logging
import sys
from typing import IO, Any, Optional

from colorlog import ColoredFormatter

from ..enum import DateFormat


[docs]def color_formatter( stream: Optional[IO[str]] = None, date_format: Optional[str] = None, **_: Any, ) -> logging.Handler: date_format = date_format or DateFormat.color.value stream = stream or sys.stderr handler = logging.StreamHandler(stream) fmt = ( "%(blue)s[T:%(threadName)s]%(reset)s " "%(log_color)s%(levelname)s:%(name)s%(reset)s: " "%(message_log_color)s%(message)s" ) if date_format: fmt = f"%(bold_white)s%(bg_black)s%(asctime)s%(reset)s {fmt}" handler.setFormatter( ColoredFormatter( fmt, log_colors={ "DEBUG": "cyan", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "red,bg_white", }, secondary_log_colors={ "message": { "WARNING": "bold", "ERROR": "bold", "CRITICAL": "bold", }, }, datefmt=date_format, reset=True, style="%", ), ) return handler