Coverage for __init__.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.7.0, created at 2025-03-20 20:51 +0100

1__version__ = "0.21.2" 

2 

3import logging 

4import sys 

5from typing import Dict, Optional 

6 

7from colorama import Back, Fore, Style 

8 

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

10# logging. source: 

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

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

13 

14 

15class 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 

36formatter = 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 

49handler = logging.StreamHandler(sys.stdout) 

50handler.setFormatter(formatter) 

51 

52logger = logging.getLogger() 

53logger.handlers[:] = [] 

54logger.addHandler(handler) 

55logger.setLevel(logging.WARNING)