Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display "exception" logs as level "error" #586

Merged
merged 4 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`.
hynek marked this conversation as resolved.
Show resolved Hide resolved
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