Skip to content

Commit e28d93b

Browse files
log formatting
1 parent 9cbba75 commit e28d93b

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

netapp_dataops_traditional/netapp_dataops/logging_utils.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,32 @@ class UserFriendlyFormatter(logging.Formatter):
44
"""Custom formatter that suppresses stack traces for user-facing messages."""
55

66
def format(self, record):
7-
if record.levelno >= logging.ERROR and record.exc_info:
8-
# Prevents stack trace from being displayed
9-
record.exc_info = None
10-
record.exc_text = None
11-
record.stack_info = None
12-
return super().format(record)
7+
try:
8+
# For ERROR and CRITICAL levels, only show the message without stack trace
9+
if record.levelno >= logging.ERROR:
10+
# Clear exc_info to prevent stack trace from being displayed
11+
record.exc_info = None
12+
record.exc_text = None
13+
record.stack_info = None
14+
15+
# Handle cases where args don't match the message format
16+
if record.args:
17+
try:
18+
# Try to format normally first
19+
record.getMessage()
20+
except (TypeError, ValueError):
21+
# If formatting fails, combine message and args manually
22+
if len(record.args) == 1:
23+
record.msg = str(record.msg) + str(record.args[0])
24+
else:
25+
record.msg = str(record.msg) + ' '.join(str(arg) for arg in record.args)
26+
record.args = None
27+
28+
return super().format(record)
29+
except Exception:
30+
# Fallback: return a simple error message
31+
return f"Error occurred: {getattr(record, 'msg', 'Unknown error')}"
32+
1333

1434
def setup_logger(name: str, level: int = logging.DEBUG) -> logging.Logger:
1535
"""

0 commit comments

Comments
 (0)