Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2019 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of puffotter. 

5 

6puffotter is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11puffotter is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with puffotter. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20import logging 

21from colorama import Back, Fore, Style 

22from typing import Optional 

23 

24 

25class ColorLogger: 

26 

27 def __init__( 

28 self, 

29 logger: logging.Logger, 

30 debug_bg: str = Back.WHITE, 

31 debug_fg: str = Fore.BLACK, 

32 info_bg: str = Back.BLACK, 

33 info_fg: str = Fore.WHITE, 

34 warning_bg: str = Back.YELLOW, 

35 warning_fg: str = Fore.BLACK, 

36 error_bg: str = Back.LIGHTRED_EX, 

37 error_fg: str = Fore.BLACK 

38 ): 

39 self.logger = logger 

40 self.debug_style = debug_bg + debug_fg 

41 self.info_style = info_bg + info_fg 

42 self.warning_style = warning_bg + warning_fg 

43 self.error_style = error_bg + error_fg 

44 

45 def __format_message( 

46 self, 

47 level: int, 

48 message: str, 

49 bg: Optional[str], 

50 fg: Optional[str] 

51 ): 

52 """ 

53 Formats a message 

54 :param level: The message's level 

55 :param message: The message to format 

56 :param bg: Optional background style override 

57 :param fg: Optional foreground style override 

58 :return: The formatted message 

59 """ 

60 style = "" 

61 if bg is not None: 

62 style += bg 

63 if fg is not None: 

64 style += fg 

65 if style == "": 

66 style = { 

67 logging.DEBUG: self.debug_style, 

68 logging.INFO: self.info_style, 

69 logging.WARNING: self.warning_style, 

70 logging.ERROR: self.error_style 

71 }[level] 

72 return style + message + Style.RESET_ALL 

73 

74 def debug( 

75 self, 

76 message: str, 

77 bg: Optional[str] = None, 

78 fg: Optional[str] = None 

79 ): 

80 """ 

81 Logs a message at DEBUG level 

82 :param message: The message to log 

83 :param bg: Overrides the default background style 

84 :param fg: Overrides the default foreground style 

85 :return: None 

86 """ 

87 self.logger.debug( 

88 self.__format_message(logging.DEBUG, message, bg, fg) 

89 ) 

90 

91 def info( 

92 self, 

93 message: str, 

94 bg: Optional[str] = None, 

95 fg: Optional[str] = None 

96 ): 

97 """ 

98 Logs a message at INFO level 

99 :param message: The message to log 

100 :param bg: Overrides the default background style 

101 :param fg: Overrides the default foreground style 

102 :return: None 

103 """ 

104 self.logger.info(self.__format_message(logging.INFO, message, bg, fg)) 

105 

106 def warning( 

107 self, 

108 message: str, 

109 bg: Optional[str] = None, 

110 fg: Optional[str] = None 

111 ): 

112 """ 

113 Logs a message at WARNING level 

114 :param message: The message to log 

115 :param bg: Overrides the default background style 

116 :param fg: Overrides the default foreground style 

117 :return: None 

118 """ 

119 self.logger.warning( 

120 self.__format_message(logging.WARNING, message, bg, fg) 

121 ) 

122 

123 def error( 

124 self, 

125 message: str, 

126 bg: Optional[str] = None, 

127 fg: Optional[str] = None 

128 ): 

129 """ 

130 Logs a message at ERROR level 

131 :param message: The message to log 

132 :param bg: Overrides the default background style 

133 :param fg: Overrides the default foreground style 

134 :return: None 

135 """ 

136 self.logger.error( 

137 self.__format_message(logging.ERROR, message, bg, fg) 

138 )