Skip to content

Commit af571c8

Browse files
committed
dev: add repr_native_str property
1 parent ad85c52 commit af571c8

File tree

4 files changed

+100
-64
lines changed

4 files changed

+100
-64
lines changed

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ API Reference
7676
.. automodule:: structlog.dev
7777

7878
.. autoclass:: ConsoleRenderer
79-
:members: get_default_level_styles, get_default_column_styles, exception_formatter, sort_keys, columns, get_active
79+
:members: get_default_level_styles, get_default_column_styles, exception_formatter, sort_keys, columns, get_active, event_key, timestamp_key, level_styles,colors, force_colors, repr_native_str, pad_level, pad_event_to
8080

8181
.. autoclass:: ColumnStyles
8282

src/structlog/dev.py

Lines changed: 79 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,38 @@ def add_meaningless_arg(arg: str) -> None:
710710
for w in to_warn:
711711
warnings.warn(w, stacklevel=2)
712712

713+
@classmethod
714+
def get_active(cls) -> ConsoleRenderer:
715+
"""
716+
If *structlog* is configured to use `ConsoleRenderer`, it's returned.
717+
718+
It does not have to be the last processor.
719+
720+
Raises:
721+
NoConsoleRendererConfiguredError:
722+
If no ConsoleRenderer is found in the current configuration.
723+
724+
MultipleConsoleRenderersConfiguredError:
725+
If more than one is found in the current configuration. This is
726+
almost certainly a bug.
727+
728+
.. versionadded:: 25.5.0
729+
"""
730+
from ._config import get_config
731+
732+
cr = None
733+
for p in get_config()["processors"]:
734+
if isinstance(p, ConsoleRenderer):
735+
if cr is not None:
736+
raise MultipleConsoleRenderersConfiguredError
737+
738+
cr = p
739+
740+
if cr is None:
741+
raise NoConsoleRendererConfiguredError
742+
743+
return cr
744+
713745
@classmethod
714746
def get_default_column_styles(
715747
cls, colors: bool, force_colors: bool = False
@@ -744,6 +776,37 @@ def get_default_column_styles(
744776

745777
return _colorful_styles
746778

779+
@staticmethod
780+
def get_default_level_styles(colors: bool = True) -> dict[str, str]:
781+
"""
782+
Get the default styles for log levels
783+
784+
This is intended to be used with `ConsoleRenderer`'s ``level_styles``
785+
parameter. For example, if you are adding custom levels in your
786+
home-grown :func:`~structlog.stdlib.add_log_level` you could do::
787+
788+
my_styles = ConsoleRenderer.get_default_level_styles()
789+
my_styles["EVERYTHING_IS_ON_FIRE"] = my_styles["critical"]
790+
renderer = ConsoleRenderer(level_styles=my_styles)
791+
792+
Args:
793+
colors:
794+
Whether to use colorful styles. This must match the *colors*
795+
parameter to `ConsoleRenderer`. Default: `True`.
796+
"""
797+
styles: ColumnStyles
798+
styles = _colorful_styles if colors else _plain_styles
799+
return {
800+
"critical": styles.level_critical,
801+
"exception": styles.level_exception,
802+
"error": styles.level_error,
803+
"warn": styles.level_warn,
804+
"warning": styles.level_warn,
805+
"info": styles.level_info,
806+
"debug": styles.level_debug,
807+
"notset": styles.level_notset,
808+
}
809+
747810
def _configure_columns(self) -> None:
748811
"""
749812
Re-configure self._columns and self._default_column_formatter
@@ -865,37 +928,6 @@ def __call__(
865928

866929
return sio.getvalue()
867930

868-
@staticmethod
869-
def get_default_level_styles(colors: bool = True) -> dict[str, str]:
870-
"""
871-
Get the default styles for log levels
872-
873-
This is intended to be used with `ConsoleRenderer`'s ``level_styles``
874-
parameter. For example, if you are adding custom levels in your
875-
home-grown :func:`~structlog.stdlib.add_log_level` you could do::
876-
877-
my_styles = ConsoleRenderer.get_default_level_styles()
878-
my_styles["EVERYTHING_IS_ON_FIRE"] = my_styles["critical"]
879-
renderer = ConsoleRenderer(level_styles=my_styles)
880-
881-
Args:
882-
colors:
883-
Whether to use colorful styles. This must match the *colors*
884-
parameter to `ConsoleRenderer`. Default: `True`.
885-
"""
886-
styles: ColumnStyles
887-
styles = _colorful_styles if colors else _plain_styles
888-
return {
889-
"critical": styles.level_critical,
890-
"exception": styles.level_exception,
891-
"error": styles.level_error,
892-
"warn": styles.level_warn,
893-
"warning": styles.level_warn,
894-
"info": styles.level_info,
895-
"debug": styles.level_debug,
896-
"notset": styles.level_notset,
897-
}
898-
899931
@property
900932
def exception_formatter(self) -> ExceptionRenderer:
901933
"""
@@ -1019,38 +1051,6 @@ def force_colors(self, value: bool) -> None:
10191051

10201052
self._configure_columns()
10211053

1022-
@classmethod
1023-
def get_active(cls) -> ConsoleRenderer:
1024-
"""
1025-
If *structlog* is configured to use `ConsoleRenderer`, it's returned.
1026-
1027-
It does not have to be the last processor.
1028-
1029-
Raises:
1030-
NoConsoleRendererConfiguredError:
1031-
If no ConsoleRenderer is found in the current configuration.
1032-
1033-
MultipleConsoleRenderersConfiguredError:
1034-
If more than one is found in the current configuration. This is
1035-
almost certainly a bug.
1036-
1037-
.. versionadded:: 25.5.0
1038-
"""
1039-
from ._config import get_config
1040-
1041-
cr = None
1042-
for p in get_config()["processors"]:
1043-
if isinstance(p, ConsoleRenderer):
1044-
if cr is not None:
1045-
raise MultipleConsoleRenderersConfiguredError
1046-
1047-
cr = p
1048-
1049-
if cr is None:
1050-
raise NoConsoleRendererConfiguredError
1051-
1052-
return cr
1053-
10541054
@property
10551055
def level_styles(self) -> dict[str, str]:
10561056
"""
@@ -1153,6 +1153,22 @@ def timestamp_key(self, value: str) -> None:
11531153
self._timestamp_key = value
11541154
self._configure_columns()
11551155

1156+
@property
1157+
def repr_native_str(self) -> bool:
1158+
"""
1159+
Whether native strings are passed through repr() in non-event values.
1160+
1161+
.. versionadded:: 25.5.0
1162+
"""
1163+
return self._repr_native_str
1164+
1165+
@repr_native_str.setter
1166+
def repr_native_str(self, value: bool) -> None:
1167+
"""
1168+
.. versionadded:: 25.5.0
1169+
"""
1170+
self._repr_native_str = value
1171+
11561172

11571173
_SENTINEL = object()
11581174

tests/test_dev.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,23 @@ def test_roundtrip_pad_event_to(self):
10041004
assert cr.pad_event_to == 20
10051005
assert cr._pad_event_to == 20
10061006

1007+
def test_repr_native_str_property(self, cr):
1008+
"""
1009+
The repr_native_str property can be set and retrieved, and affects formatting.
1010+
"""
1011+
cr = dev.ConsoleRenderer(colors=False, repr_native_str=False)
1012+
1013+
assert False is cr.repr_native_str
1014+
assert "event key=plain" == cr(
1015+
None, None, {"event": "event", "key": "plain"}
1016+
)
1017+
1018+
cr.repr_native_str = True
1019+
1020+
assert "event key='plain'" == cr(
1021+
None, None, {"event": "event", "key": "plain"}
1022+
)
1023+
10071024
def test_pad_event_deprecation_warning(self, recwarn):
10081025
"""
10091026
Using pad_event argument raises a deprecation warning.

tests/typing/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,6 @@ def f() -> None:
408408

409409
cr.event_key
410410
cr.event_key = "le event"
411+
412+
cr.repr_native_str
413+
cr.repr_native_str = True

0 commit comments

Comments
 (0)