Coverage for mymeco/logger.py: 100%

17 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-12-11 22:07 +0000

1# coding: utf-8 

2""" 

3Logging utility module. 

4 

5Module aims to configure logging (verbosity, format, etc…) 

6""" 

7import typing 

8import logging 

9import coloredlogs 

10 

11 

12def configure(verbosity_count: int = 0, 

13 quiet_count: int = 0, 

14 colored: bool = True) -> None: 

15 """ 

16 Configure main logger according to given verbosity configuration. 

17 

18 :param verbosity_count: Set number of `-v` command line switch. 

19 :param quiet_count: Set number of `-q` command line switch. 

20 :param colored: Define if log output should be colored or not. 

21 

22 Default is to set verbosity on INFO level 

23 

24 """ 

25 log = logging.getLogger() 

26 

27 # Compute verbosity: 10 is for DEBUG and 50 for CRITICAL 

28 # Default is 20 (for INFO). Each verbosity count (-v) will decrease 

29 # level by 10 and each quiet count (-q) will increase level by 10. 

30 level = 20 - (verbosity_count * 10) + (quiet_count * 10) 

31 level = max(0, min(60, level)) 

32 

33 log.setLevel(level) 

34 log.handlers = [] 

35 

36 formatter_class: typing.Type[logging.Formatter] 

37 if colored: 

38 formatter_class = coloredlogs.ColoredFormatter 

39 else: 

40 formatter_class = logging.Formatter 

41 

42 handler = logging.StreamHandler() 

43 handler.setLevel(logging.DEBUG) 

44 handler.setFormatter( 

45 formatter_class('%(levelname)s\t%(message)s') 

46 ) 

47 

48 log.addHandler(handler)