Skip to content

Commit a8c3d84

Browse files
committed
Add padding to levelname in terminal logs
It makes it easier to follow logs when the message part is aligned and the level does not affect where the text starts. The colouring method has been refactored at the same time to reduce the duplication around adding styles.
1 parent 18489bd commit a8c3d84

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/blueapi/log.py

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -139,34 +139,25 @@ class IBMColorBlindSafeColors(enum.Enum):
139139
class ColorFormatter(logging.Formatter):
140140
"""Colors level_name of log using IBM color blind safe palette."""
141141

142-
def color_level_name(self, level_name: str, level_no: int) -> str:
142+
def _level_colour(self, level_no: int) -> tuple[int, int, int] | None:
143143
match level_no:
144144
case logging.DEBUG:
145-
return click.style(
146-
str(level_name), fg=IBMColorBlindSafeColors.ultramarine.value
147-
)
145+
return IBMColorBlindSafeColors.ultramarine.value
148146
case logging.INFO:
149-
return click.style(
150-
str(level_name), fg=IBMColorBlindSafeColors.indigo.value
151-
)
147+
return IBMColorBlindSafeColors.indigo.value
152148
case logging.WARNING:
153-
return click.style(
154-
str(level_name), fg=IBMColorBlindSafeColors.gold.value
155-
)
149+
return IBMColorBlindSafeColors.gold.value
156150
case logging.ERROR:
157-
return click.style(
158-
str(level_name), fg=IBMColorBlindSafeColors.magenta.value
159-
)
151+
return IBMColorBlindSafeColors.magenta.value
160152
case logging.CRITICAL:
161-
return click.style(
162-
str(level_name), fg=IBMColorBlindSafeColors.orange.value
163-
)
164-
return level_name
153+
return IBMColorBlindSafeColors.orange.value
154+
case _:
155+
return None
165156

166157
def formatMessage(self, record: logging.LogRecord) -> str: # noqa: N802
167158
# Copy record to avoid modifying for other handlers etc.
168159
recordcopy = copy(record)
169-
recordcopy.levelname = self.color_level_name(
170-
recordcopy.levelname, recordcopy.levelno
160+
recordcopy.levelname = click.style(
161+
f"{recordcopy.levelname:>7}", fg=self._level_colour(recordcopy.levelno)
171162
)
172163
return super().formatMessage(recordcopy)

0 commit comments

Comments
 (0)