Skip to content

Commit 163ec72

Browse files
fix: Separate formatter from logger to avoid loading singleton
1 parent eddc065 commit 163ec72

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

src/ansys/tools/common/logger.py

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import datetime
2727
import logging
2828
from pathlib import Path
29-
29+
from src.ansys.tools.common.logger_formatter import DEFAULT_FORMATTER
3030

3131
class SingletonType(type):
3232
"""Provides the singleton helper class for the logger."""
@@ -40,53 +40,6 @@ def __call__(cls, *args, **kwargs):
4040
return cls._instances[cls]
4141

4242

43-
class CustomFormatter(logging.Formatter):
44-
"""Custom formatter to truncate long columns."""
45-
46-
def set_column_width(self, width: int):
47-
"""Set the maximum column width for module and function names."""
48-
# at least 8
49-
if width < 8:
50-
raise ValueError("Column width must be at least 8 characters.")
51-
self._max_column_width = width
52-
53-
@property
54-
def max_column_width(self):
55-
"""Get the maximum column length."""
56-
if not hasattr(self, "_max_column_width"):
57-
self._max_column_width = 15
58-
return self._max_column_width
59-
60-
def format(self, record):
61-
"""Format the log record, truncating the module and function names if necessary."""
62-
if len(record.module) > self.max_column_width:
63-
record.module = record.module[: self.max_column_width - 3] + "..."
64-
if len(record.funcName) > self.max_column_width:
65-
record.funcName = record.funcName[: self.max_column_width - 3] + "..."
66-
67-
# Fill the module and function names with spaces to align them
68-
record.module = record.module.ljust(self.max_column_width)
69-
record.funcName = record.funcName.ljust(self.max_column_width)
70-
71-
return super().format(record)
72-
73-
74-
DEFAULT_FORMATTER = CustomFormatter(
75-
"%(asctime)s [%(levelname)-8s | %(module)s | %(funcName)s:%(lineno)-4d] > %(message)s"
76-
)
77-
DEFAULT_FORMATTER.set_column_width(15)
78-
"""Default formatter for the logger."""
79-
80-
DEFAULT_HEADER = (
81-
"-" * (70 + DEFAULT_FORMATTER.max_column_width)
82-
+ "\n"
83-
+ f"Timestamp [Level | Module{' ' * (DEFAULT_FORMATTER.max_column_width - 6)} | Function{' ' * (DEFAULT_FORMATTER.max_column_width - 8)}:Line] > Message\n" # noqa: E501
84-
+ "-" * (70 + DEFAULT_FORMATTER.max_column_width)
85-
+ "\n"
86-
)
87-
"""Default header for the log file."""
88-
89-
9043
class Logger(object, metaclass=SingletonType):
9144
"""Provides the singleton logger.
9245
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""Default logger formatter module."""
2+
import logging
3+
4+
5+
class CustomFormatter(logging.Formatter):
6+
"""Custom formatter to truncate long columns."""
7+
8+
def set_column_width(self, width: int):
9+
"""Set the maximum column width for module and function names."""
10+
# at least 8
11+
if width < 8:
12+
raise ValueError("Column width must be at least 8 characters.")
13+
self._max_column_width = width
14+
15+
@property
16+
def max_column_width(self):
17+
"""Get the maximum column length."""
18+
if not hasattr(self, "_max_column_width"):
19+
self._max_column_width = 15
20+
return self._max_column_width
21+
22+
def format(self, record):
23+
"""Format the log record, truncating the module and function names if necessary."""
24+
if len(record.module) > self.max_column_width:
25+
record.module = record.module[: self.max_column_width - 3] + "..."
26+
if len(record.funcName) > self.max_column_width:
27+
record.funcName = record.funcName[: self.max_column_width - 3] + "..."
28+
29+
# Fill the module and function names with spaces to align them
30+
record.module = record.module.ljust(self.max_column_width)
31+
record.funcName = record.funcName.ljust(self.max_column_width)
32+
33+
return super().format(record)
34+
35+
36+
DEFAULT_FORMATTER = CustomFormatter(
37+
"%(asctime)s [%(levelname)-8s | %(module)s | %(funcName)s:%(lineno)-4d] > %(message)s"
38+
)
39+
DEFAULT_FORMATTER.set_column_width(15)
40+
"""Default formatter for the logger."""
41+
42+
DEFAULT_HEADER = (
43+
"-" * (70 + DEFAULT_FORMATTER.max_column_width)
44+
+ "\n"
45+
+ f"Timestamp [Level | Module{' ' * (DEFAULT_FORMATTER.max_column_width - 6)} | Function{' ' * (DEFAULT_FORMATTER.max_column_width - 8)}:Line] > Message\n" # noqa: E501
46+
+ "-" * (70 + DEFAULT_FORMATTER.max_column_width)
47+
+ "\n"
48+
)
49+
"""Default header for the log file."""

0 commit comments

Comments
 (0)