MCPcopy
hub / github.com/treeverse/dvc / ColorFormatter

Class ColorFormatter

dvc/logger.py:69–132  ·  view source on GitHub ↗

Spit out colored text in supported terminals. colorama__ makes ANSI escape character sequences work under Windows. See the colorama documentation for details. __ https://pypi.python.org/pypi/colorama If record has an extra `tb_only` attribute, it will not show the exception ca

Source from the content-addressed store, hash-verified

67
68
69class ColorFormatter(logging.Formatter):
70 """Spit out colored text in supported terminals.
71
72 colorama__ makes ANSI escape character sequences work under Windows.
73 See the colorama documentation for details.
74
75 __ https://pypi.python.org/pypi/colorama
76
77 If record has an extra `tb_only` attribute, it will not show the
78 exception cause, just the message and the traceback.
79 """
80
81 reset = colorama.Fore.RESET
82 color_codes: ClassVar[dict[str, str]] = {
83 "TRACE": colorama.Fore.GREEN,
84 "DEBUG": colorama.Fore.BLUE,
85 "WARNING": colorama.Fore.YELLOW,
86 "ERROR": colorama.Fore.RED,
87 "CRITICAL": colorama.Fore.RED,
88 }
89
90 def __init__(self, log_colors: bool = True, show_traceback: bool = False) -> None:
91 super().__init__()
92 self.log_colors = log_colors
93 self.show_traceback = show_traceback
94
95 def format(self, record) -> str: # noqa: C901
96 record.message = record.getMessage()
97 msg = self.formatMessage(record)
98
99 if record.levelno == logging.INFO:
100 return msg
101
102 ei = record.exc_info
103 if ei:
104 cause = ""
105 if not getattr(record, "tb_only", False):
106 cause = ": ".join(_iter_causes(ei[1]))
107 sep = " - " if msg and cause else ""
108 msg = msg + sep + cause
109
110 asctime = ""
111 verbose = _is_verbose()
112 if verbose:
113 asctime = self.formatTime(record, self.datefmt)
114 if verbose or self.show_traceback:
115 if ei and not record.exc_text:
116 record.exc_text = self.formatException(ei)
117 if record.exc_text:
118 if msg[-1:] != "\n":
119 msg = msg + "\n"
120 msg = msg + record.exc_text + "\n"
121 if record.stack_info:
122 if msg[-1:] != "\n":
123 msg = msg + "\n"
124 msg = msg + self.formatStack(record.stack_info) + "\n"
125
126 level = record.levelname

Callers 1

setupFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected