Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/colorlog/logging.py: 69%

29 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

1"""Wrappers around the logging module.""" 

2 

3from __future__ import absolute_import 

4 

5import functools 

6import logging 

7 

8from colorlog.colorlog import ColoredFormatter 

9 

10BASIC_FORMAT = "%(log_color)s%(levelname)s%(reset)s:%(name)s:%(message)s" 

11 

12 

13def basicConfig( 

14 style="%", 

15 log_colors=None, 

16 reset=True, 

17 secondary_log_colors=None, 

18 format=BASIC_FORMAT, 

19 datefmt=None, 

20 **kwargs 

21): 

22 """Call ``logging.basicConfig`` and override the formatter it creates.""" 

23 logging.basicConfig(**kwargs) 

24 logging._acquireLock() 

25 try: 

26 stream = logging.root.handlers[0] 

27 stream.setFormatter( 

28 ColoredFormatter( 

29 fmt=format, 

30 datefmt=datefmt, 

31 style=style, 

32 log_colors=log_colors, 

33 reset=reset, 

34 secondary_log_colors=secondary_log_colors, 

35 ) 

36 ) 

37 finally: 

38 logging._releaseLock() 

39 

40 

41def ensure_configured(func): 

42 """Modify a function to call ``basicConfig`` first if no handlers exist.""" 

43 

44 @functools.wraps(func) 

45 def wrapper(*args, **kwargs): 

46 if len(logging.root.handlers) == 0: 

47 basicConfig() 

48 return func(*args, **kwargs) 

49 

50 return wrapper 

51 

52 

53root = logging.root 

54getLogger = logging.getLogger 

55debug = ensure_configured(logging.debug) 

56info = ensure_configured(logging.info) 

57warning = ensure_configured(logging.warning) 

58error = ensure_configured(logging.error) 

59critical = ensure_configured(logging.critical) 

60log = ensure_configured(logging.log) 

61exception = ensure_configured(logging.exception) 

62 

63StreamHandler = logging.StreamHandler