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

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

27 statements  

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

2 

3import functools 

4import logging 

5import typing 

6from logging import ( 

7 CRITICAL, 

8 DEBUG, 

9 ERROR, 

10 FATAL, 

11 INFO, 

12 NOTSET, 

13 StreamHandler, 

14 WARN, 

15 WARNING, 

16 getLogger, 

17 root, 

18) 

19 

20import colorlog.formatter 

21 

22__all__ = ( 

23 "CRITICAL", 

24 "DEBUG", 

25 "ERROR", 

26 "FATAL", 

27 "INFO", 

28 "NOTSET", 

29 "WARN", 

30 "WARNING", 

31 "StreamHandler", 

32 "basicConfig", 

33 "critical", 

34 "debug", 

35 "error", 

36 "exception", 

37 "getLogger", 

38 "info", 

39 "log", 

40 "root", 

41 "warning", 

42) 

43 

44 

45def basicConfig( 

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

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

48 reset: bool = True, 

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

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

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

52 **kwargs 

53) -> None: 

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

55 logging.basicConfig(**kwargs) 

56 logging._acquireLock() # type: ignore 

57 try: 

58 handler = logging.root.handlers[0] 

59 handler.setFormatter( 

60 colorlog.formatter.ColoredFormatter( 

61 fmt=format, 

62 datefmt=datefmt, 

63 style=style, 

64 log_colors=log_colors, 

65 reset=reset, 

66 secondary_log_colors=secondary_log_colors, 

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

68 ) 

69 ) 

70 finally: 

71 logging._releaseLock() # type: ignore 

72 

73 

74def ensure_configured(func): 

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

76 

77 @functools.wraps(func) 

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

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

80 basicConfig() 

81 return func(*args, **kwargs) 

82 

83 return wrapper 

84 

85 

86debug = ensure_configured(logging.debug) 

87info = ensure_configured(logging.info) 

88warning = ensure_configured(logging.warning) 

89error = ensure_configured(logging.error) 

90critical = ensure_configured(logging.critical) 

91log = ensure_configured(logging.log) 

92exception = ensure_configured(logging.exception)