Skip to content

Commit 0a64815

Browse files
begoonpre-commit-ci[bot]hynek
authored
Add skip_level_padding to ConsoleRenderer (#599)
* Add skip_level_padding to ConsoleRenderer * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Re-use existing width argument * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix src/structlog/dev.py:545:9: PLR0915 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix accidentally duplicated "color_system" * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update src/structlog/dev.py * Update src/structlog/dev.py * Update src/structlog/dev.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Hynek Schlawack <[email protected]>
1 parent 6b82c5c commit 0a64815

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/structlog/dev.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,18 +283,29 @@ class LogLevelColumnFormatter:
283283
What to use to reset the style after the level name. Ignored if
284284
if *level_styles* is None.
285285
286+
width:
287+
The width to pad the level to. If 0, no padding is done.
288+
286289
.. versionadded:: 23.3.0
290+
.. versionadded:: 24.2.0 *width*
287291
"""
288292

289293
level_styles: dict[str, str] | None
290294
reset_style: str
291295
width: int
292296

293-
def __init__(self, level_styles: dict[str, str], reset_style: str) -> None:
297+
def __init__(
298+
self,
299+
level_styles: dict[str, str],
300+
reset_style: str,
301+
width: int | None = None,
302+
) -> None:
294303
self.level_styles = level_styles
295304
if level_styles:
296-
self.width = len(
297-
max(self.level_styles.keys(), key=lambda e: len(e))
305+
self.width = (
306+
0
307+
if width == 0
308+
else len(max(self.level_styles.keys(), key=lambda e: len(e)))
298309
)
299310
self.reset_style = reset_style
300311
else:
@@ -485,6 +496,10 @@ class ConsoleRenderer:
485496
rename it e.g. using `structlog.processors.EventRenamer`. Ignored
486497
if *columns* are passed.
487498
499+
pad_level:
500+
Whether to pad log level with blanks to the longest amongst all
501+
level label.
502+
488503
Requires the Colorama_ package if *colors* is `True` **on Windows**.
489504
490505
Raises:
@@ -524,9 +539,10 @@ class ConsoleRenderer:
524539
.. versionadded:: 22.1.0 *event_key*
525540
.. versionadded:: 23.2.0 *timestamp_key*
526541
.. versionadded:: 23.3.0 *columns*
542+
.. versionadded:: 24.2.0 *pad_level*
527543
"""
528544

529-
def __init__( # noqa: PLR0912
545+
def __init__( # noqa: PLR0912, PLR0915
530546
self,
531547
pad_event: int = _EVENT_WIDTH,
532548
colors: bool = _has_colors,
@@ -538,6 +554,7 @@ def __init__( # noqa: PLR0912
538554
event_key: str = "event",
539555
timestamp_key: str = "timestamp",
540556
columns: list[Column] | None = None,
557+
pad_level: bool = True,
541558
):
542559
self._exception_formatter = exception_formatter
543560
self._sort_keys = sort_keys
@@ -645,6 +662,8 @@ def add_meaningless_arg(arg: str) -> None:
645662
postfix="]",
646663
)
647664

665+
level_width = 0 if not pad_level else None
666+
648667
self._columns = [
649668
Column(
650669
timestamp_key,
@@ -658,7 +677,7 @@ def add_meaningless_arg(arg: str) -> None:
658677
Column(
659678
"level",
660679
LogLevelColumnFormatter(
661-
level_to_color, reset_style=styles.reset
680+
level_to_color, reset_style=styles.reset, width=level_width
662681
),
663682
),
664683
Column(

0 commit comments

Comments
 (0)