Skip to content

Commit f4c0980

Browse files
committed
When logging exceptions, enable colorized traceback that were introduced in python3.13.
1 parent dfa10f5 commit f4c0980

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

colorlog/formatter.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import os
55
import sys
66
import typing
7+
import traceback
8+
import io
79

810
import colorlog.escape_codes
911

@@ -175,6 +177,29 @@ def _append_reset(self, message: str, escapes: EscapeCodes) -> str:
175177

176178
return message
177179

180+
if sys.version_info >= (3, 13):
181+
182+
def formatException(self, ei):
183+
"""Format and return the specified exception information as a string."""
184+
# This is a copy of logging.Formatter.formatException that passes in
185+
# an appropriate value for colorize to print_exception.
186+
187+
sio = io.StringIO()
188+
tb = ei[2]
189+
traceback.print_exception(
190+
ei[0],
191+
ei[1],
192+
tb,
193+
limit=None,
194+
file=sio,
195+
colorize=not self._blank_escape_codes(),
196+
)
197+
s = sio.getvalue()
198+
sio.close()
199+
if s[-1:] == "\n":
200+
s = s[:-1]
201+
return s
202+
178203

179204
class LevelFormatter:
180205
"""An extension of ColoredFormatter that uses per-level format strings."""

0 commit comments

Comments
 (0)