Skip to content

Commit ac932d3

Browse files
committed
improve reliability of log formatter when exceptions happen in 3rd party modules
1 parent cebc132 commit ac932d3

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

ccos/log.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ def update_format(self, record):
6464
"%(asctime)s │ "
6565
f"{prefix}{color}%(levelname)-8s{prefix}{reset} │ "
6666
)
67-
self._style._fmt += (
68-
f"%(indent)s{prefix}{bold}%(function)s{prefix}{reset}: "
69-
"%(message)s"
70-
)
67+
if hasattr(record, "function"):
68+
self._style._fmt += (
69+
f"%(indent)s{prefix}{bold}%(function)s{prefix}{reset}: "
70+
"%(message)s"
71+
)
72+
else:
73+
self._style._fmt += f"%(indent)s%(message)s"
74+
7175

7276
def format(self, record):
7377
"""
@@ -81,19 +85,21 @@ def format(self, record):
8185
self.baseline = depth
8286
if self.cut is None:
8387
filenames = map(lambda x: x.filename, stack)
84-
self.cut = IndentFormatter.identify_cut(filenames)
88+
self.cut = self.identify_cut(filenames)
8589

8690
# Inject custom information into the record
8791
record.indent = ". " * (depth - self.baseline + self.manual_push)
88-
record.function = stack[self.cut].function
92+
if depth > self.cut:
93+
record.function = stack[self.cut].function
8994

9095
# Format the record using custom information
9196
self.update_format(record)
9297
out = super().format(record)
9398

9499
# Remove custom information from the record
95100
del record.indent
96-
del record.function
101+
if hasattr(record, "function"):
102+
del record.function
97103

98104
return out
99105

0 commit comments

Comments
 (0)