Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/colorlog/wrappers.py: 59%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

34 statements  

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

2 

3import functools 

4import logging 

5import sys 

6import typing 

7from logging import ( 

8 CRITICAL, 

9 DEBUG, 

10 ERROR, 

11 FATAL, 

12 INFO, 

13 NOTSET, 

14 StreamHandler, 

15 WARN, 

16 WARNING, 

17 getLogger, 

18 root, 

19) 

20 

21import colorlog.formatter 

22 

23__all__ = ( 

24 "CRITICAL", 

25 "DEBUG", 

26 "ERROR", 

27 "FATAL", 

28 "INFO", 

29 "NOTSET", 

30 "WARN", 

31 "WARNING", 

32 "StreamHandler", 

33 "basicConfig", 

34 "critical", 

35 "debug", 

36 "error", 

37 "exception", 

38 "getLogger", 

39 "info", 

40 "log", 

41 "root", 

42 "warning", 

43) 

44 

45 

46def basicConfig( 

47 style: colorlog.formatter._FormatStyle = "%", 

48 log_colors: typing.Optional[colorlog.formatter.LogColors] = None, 

49 reset: bool = True, 

50 secondary_log_colors: typing.Optional[colorlog.formatter.SecondaryLogColors] = None, 

51 format: str = "%(log_color)s%(levelname)s%(reset)s:%(name)s:%(message)s", 

52 datefmt: typing.Optional[str] = None, 

53 **kwargs 

54) -> None: 

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

56 logging.basicConfig(**kwargs) 

57 

58 def _basicConfig(): 

59 handler = logging.root.handlers[0] 

60 handler.setFormatter( 

61 colorlog.formatter.ColoredFormatter( 

62 fmt=format, 

63 datefmt=datefmt, 

64 style=style, 

65 log_colors=log_colors, 

66 reset=reset, 

67 secondary_log_colors=secondary_log_colors, 

68 stream=kwargs.get("stream", None), 

69 ) 

70 ) 

71 

72 if sys.version_info >= (3, 13): 

73 with logging._lock: # type: ignore 

74 _basicConfig() 

75 else: 

76 logging._acquireLock() # type: ignore 

77 try: 

78 _basicConfig() 

79 finally: 

80 logging._releaseLock() # type: ignore 

81 

82 

83def ensure_configured(func): 

84 """Modify a function to call our basicConfig() first if no handlers exist.""" 

85 

86 @functools.wraps(func) 

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

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

89 basicConfig() 

90 return func(*args, **kwargs) 

91 

92 return wrapper 

93 

94 

95debug = ensure_configured(logging.debug) 

96info = ensure_configured(logging.info) 

97warning = ensure_configured(logging.warning) 

98error = ensure_configured(logging.error) 

99critical = ensure_configured(logging.critical) 

100log = ensure_configured(logging.log) 

101exception = ensure_configured(logging.exception)