Skip to content

Commit

Permalink
Allow look for logger_name in ConsoleRenderer
Browse files Browse the repository at this point in the history
Fixes #295
  • Loading branch information
hynek committed Feb 18, 2021
1 parent 7169709 commit b0be044
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Changes:
`#302 <https://github.com/hynek/structlog/pull/302>`_
- The default configuration and loggers are pickleable again.
`#301 <https://github.com/hynek/structlog/pull/301>`_
- ``structlog.dev.ConsoleRenderer`` will now look for a ``logger_name`` key if no
``logger`` key is set.
`#295 <https://github.com/hynek/structlog/pull/295>`_


----
Expand Down
5 changes: 5 additions & 0 deletions src/structlog/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class ConsoleRenderer:
.. versionchanged:: 19.2 Can be pickled now.
.. versionchanged:: 20.1 ``colorama`` does not initialize lazily on Windows
anymore because it breaks rendering.
.. versionchanged: 21.1 It is additionally possible to set the logger name
using the ``logger_name`` key in the ``event_dict``.
"""

def __init__(
Expand Down Expand Up @@ -255,6 +257,9 @@ def __call__(
sio.write(self._styles.bright + event)

logger_name = event_dict.pop("logger", None)
if logger_name is None:
logger_name = event_dict.pop("logger_name", None)

if logger_name is not None:
sio.write(
"["
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
)


@pytest.fixture
def proxy():
@pytest.fixture(name="proxy")
def _proxy():
"""
Returns a BoundLoggerLazyProxy constructed w/o parameters & None as logger.
"""
Expand Down
43 changes: 29 additions & 14 deletions tests/test_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ def test_negative(self):
assert len("test") == len(dev._pad("test", 2))


@pytest.fixture
def cr():
@pytest.fixture(name="cr")
def _cr():
return dev.ConsoleRenderer(colors=dev._has_colorama)


@pytest.fixture
def styles(cr):
@pytest.fixture(name="styles")
def _styles(cr):
return cr._styles


@pytest.fixture
def padded(styles):
@pytest.fixture(name="padded")
def _padded(styles):
return (
styles.bright + dev._pad("test", dev._EVENT_WIDTH) + styles.reset + " "
)


@pytest.fixture
def unpadded(styles):
@pytest.fixture(name="unpadded")
def _unpadded(styles):
return styles.bright + "test" + styles.reset


Expand Down Expand Up @@ -139,14 +139,29 @@ def test_logger_name(self, cr, styles, padded):
"""
rv = cr(None, None, {"event": "test", "logger": "some_module"})

# fmt: off
assert (
padded +
"[" + dev.BLUE + styles.bright +
"some_module" +
styles.reset + "] "
padded
+ "["
+ dev.BLUE
+ styles.bright
+ "some_module"
+ styles.reset
+ "] "
) == rv
# fmt: on

def test_logger_name_name(self, cr, padded, styles):
"""
It's possible to set the logger name using a "logger_name" key.
"""
assert (
padded
+ "["
+ dev.BLUE
+ styles.bright
+ "yolo"
+ styles.reset
+ "] "
) == cr(None, None, {"event": "test", "logger_name": "yolo"})

def test_key_values(self, cr, styles, padded):
"""
Expand Down

0 comments on commit b0be044

Please sign in to comment.