Skip to content

Commit

Permalink
Display "exception" logs as level "error" (#586)
Browse files Browse the repository at this point in the history
* Display "exception" logs as level "error"

* Update CHANGELOG.md

* Update src/structlog/_log_levels.py

---------

Co-authored-by: Hynek Schlawack <hs@ox.cx>
  • Loading branch information
kimsappi and hynek authored Jan 8, 2024
1 parent ac37281 commit 0b7da28
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
- The lazy logger proxy returned by `structlog.get_logger()` now returns its initial values when asked for context.
When asked for context before binding for the first time, it returned an empty dictionary in 23.3.0.

- The displayed level name when using `structlog.stdlib.BoundLogger.exception()` is `"error"` instead of `"exception"`.
Fixes regression in 23.3.0.
[#584](https://github.com/hynek/structlog/issues/584)

- Don't ignore the `width` argument of `RichTracebackFormatter`.
[#587](https://github.com/hynek/structlog/issues/587)

Expand Down
5 changes: 5 additions & 0 deletions src/structlog/_log_levels.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ def add_log_level(
.. versionchanged:: 20.2.0
Importable from `structlog.processors` (additionally to
`structlog.stdlib`).
.. versionchanged:: 24.1.0
Added mapping from "exception" to "error"
"""
if method_name == "warn":
# The stdlib has an alias
method_name = "warning"
elif method_name == "exception":
# exception("") method is the same as error("", exc_info=True)
method_name = "error"

event_dict["level"] = method_name

Expand Down
9 changes: 6 additions & 3 deletions tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,16 @@ def test_log_level_added(self):

assert "error" == event_dict["level"]

def test_log_level_alias_normalized(self):
@pytest.mark.parametrize(
("alias", "normalized"), [("warn", "warning"), ("exception", "error")]
)
def test_log_level_alias_normalized(self, alias, normalized):
"""
The normalized name of the log level is added to the event dict.
"""
event_dict = add_log_level(None, "warn", {})
event_dict = add_log_level(None, alias, {})

assert "warning" == event_dict["level"]
assert normalized == event_dict["level"]


@pytest.fixture(name="make_log_record")
Expand Down

0 comments on commit 0b7da28

Please sign in to comment.