Coverage for __init__.py: 100%

1 __version__ = "0.21.2"

2

3 import logging

4 import sys

5 from typing import Dict, Optional

6

7 from colorama import Back, Fore, Style

8

9# =============================================================================

10# logging. source:

11# https://gist.github.com/joshbode/58fac7ababc700f51e2a9ecdebe563ad

12# =============================================================================

13

14

15 class ColoredFormatter(logging.Formatter):

16 """Colored log formatter."""

17

18 def __init__(

19 self, *args, colors: Optional[Dict[str, str]] = None, **kwargs

20 ) -> None:

21 """Initialize the formatter with specified format strings."""

22

23 super().__init__(*args, **kwargs)

24

25 self.colors = colors if colors else {}

26

27 def format(self, record) -> str:

28 """Format the specified record as text."""

29

30 record.color = self.colors.get(record.levelname, "")

31 record.reset = Style.RESET_ALL

32

33 return super().format(record)

34

35

36 formatter = ColoredFormatter(

37 "{asctime} |{color} {levelname:8} {reset}| {name} | {message}",

38 style="{",

39 datefmt="%Y-%m-%d %H:%M:%S",

40 colors={

41 "DEBUG": Fore.CYAN,

42 "INFO": Fore.GREEN,

43 "WARNING": Fore.YELLOW,

44 "ERROR": Fore.RED,

45 "CRITICAL": Fore.RED + Back.WHITE + Style.BRIGHT,

46 },

47)

48

49 handler = logging.StreamHandler(sys.stdout)

50 handler.setFormatter(formatter)

51

52 logger = logging.getLogger()

53 logger.handlers[:] = []

54 logger.addHandler(handler)

55 logger.setLevel(logging.WARNING)