diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fa4835..d375c4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). The format is based on [Keep a Changelog](http://keepachangelog.com/). ## Unreleased +### Changed + +- On Python 3.12+, `taskName` is no longer written as `null` when a record is logged outside an asyncio task. It + is still included, with the task name, for records logged inside one (e.g. `async def` FastAPI endpoints). + Consumers that expect the field on every record should treat it as optional. (fixes #157) + ## 1.7.0rc0 - 2026-11-10 ### Added diff --git a/README.md b/README.md index 215ddd8..b807fa5 100644 --- a/README.md +++ b/README.md @@ -561,6 +561,7 @@ See the following tables for a detailed explanation of the format: | line_no | The line number of the logging call | integer | 22 | | filename | The file where an exception originated, only present when an exception is logged | string | /path/to/foo.py | | exc_info | Traceback information about an exception | string | "Traceback (most recent call last): \n File "<stdin>", line 1, in <module>\n ValueError: There is something wrong with your input" | +| taskName | Name of the asyncio task that wrote the log message, only present on Python 3.12+ when logged inside an asyncio task | string | Task-1 | | stack_info | Call stack of the logging statement, only present when it was called with `stack_info=True` | string | "Stack (most recent call last):\n File "app.py", line 10, in <module>\n logger.info("hello", stack_info=True)" | **Request logs** diff --git a/json_logging/formatters.py b/json_logging/formatters.py index 7fcab2e..2824d8a 100644 --- a/json_logging/formatters.py +++ b/json_logging/formatters.py @@ -28,6 +28,9 @@ LOG_RECORD_BUILT_IN_ATTRS.append('stack_info') EASY_SERIALIZABLE_TYPES = (str, bool, dict, float, int, list, type(None)) +# LogRecord.taskName is a built-in attribute from Python 3.12; extra cannot set it there +OMIT_NULL_TASK_NAME = sys.version_info >= (3, 12) + def _sanitize_log_msg(record): """ @@ -113,6 +116,11 @@ def _get_extra_fields(self, record): # try to cast it to a string representation fields[key] = repr(value) + # Python 3.12+ sets taskName on every record; it is None outside an asyncio task, so leave it out there. + # Before 3.12 taskName can only come from extra, so it is left alone. + if OMIT_NULL_TASK_NAME and 'taskName' in fields and fields['taskName'] is None: + del fields['taskName'] + # Always add 'props' to the root of the log, assumes props is a dict if hasattr(record, 'props') and isinstance(record.props, dict): fields.update(record.props) diff --git a/tests/conftest.py b/tests/conftest.py index bedebf4..a3e80b9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,10 +1,6 @@ """Global fixtures and settings for the pytest test suite""" import sys import os -from helpers import constants # Add test helper modules to search path with out making "tests" a Python package sys.path.append(os.path.join(os.path.dirname(__file__), "helpers")) - -if sys.version_info.major > 3 or (sys.version_info.major == 3 and sys.version_info.minor >= 12): - constants.STANDARD_MSG_ATTRIBUTES.add('taskName') diff --git a/tests/helpers/constants.py b/tests/helpers/constants.py index 9a355ba..b64fae8 100644 --- a/tests/helpers/constants.py +++ b/tests/helpers/constants.py @@ -1,4 +1,5 @@ """Constants shared by multiple tests""" +import sys STANDARD_MSG_ATTRIBUTES = { "written_at", @@ -12,3 +13,6 @@ "line_no", "correlation_id", } + +# Python 3.12+ fills in taskName for records logged inside an asyncio task; it is left out when it is None +ASYNC_TASK_MSG_ATTRIBUTES = STANDARD_MSG_ATTRIBUTES | ({"taskName"} if sys.version_info >= (3, 12) else set()) diff --git a/tests/test_connexion_async.py b/tests/test_connexion_async.py index eba1b3f..054c4f9 100644 --- a/tests/test_connexion_async.py +++ b/tests/test_connexion_async.py @@ -98,7 +98,8 @@ def test_handler_logs_share_the_request_correlation_id(client_and_log_handlers, correlation_id = response.json()["correlation_id"] assert len(handler.messages) == 1 msg = json.loads(handler.messages[0]) - assert set(msg.keys()) == constants.STANDARD_MSG_ATTRIBUTES + expected = constants.ASYNC_TASK_MSG_ATTRIBUTES if path == "/log/async" else constants.STANDARD_MSG_ATTRIBUTES + assert set(msg.keys()) == expected assert msg["correlation_id"] == correlation_id assert json.loads(request_handler.messages[0])["correlation_id"] == correlation_id diff --git a/tests/test_fastapi.py b/tests/test_fastapi.py index 40970a0..6780a86 100644 --- a/tests/test_fastapi.py +++ b/tests/test_fastapi.py @@ -120,7 +120,7 @@ def test_record_format_per_log_level(client_and_log_handler, level): assert response.status_code == 200 assert len(handler.messages) == 1 msg = json.loads(handler.messages[0]) - assert set(msg.keys()) == constants.STANDARD_MSG_ATTRIBUTES + assert set(msg.keys()) == constants.ASYNC_TASK_MSG_ATTRIBUTES assert msg["module"] == __name__ assert msg["level"] == level.upper() assert msg["logger"] == LOGGER_NAME @@ -140,7 +140,7 @@ def test_correlation_id_given(client_and_log_handler): assert response.status_code == 200 assert len(handler.messages) == 1 msg = json.loads(handler.messages[0]) - assert set(msg.keys()) == constants.STANDARD_MSG_ATTRIBUTES + assert set(msg.keys()) == constants.ASYNC_TASK_MSG_ATTRIBUTES assert msg["correlation_id"] == "abc-def" @@ -153,7 +153,7 @@ def test_correlation_id_generated(client_and_log_handler): assert response.status_code == 200 assert len(handler.messages) == 1 msg = json.loads(handler.messages[0]) - assert set(msg.keys()) == constants.STANDARD_MSG_ATTRIBUTES + assert set(msg.keys()) == constants.ASYNC_TASK_MSG_ATTRIBUTES assert re.match( r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$", msg["correlation_id"], @@ -477,7 +477,7 @@ def test_extra_property(client_and_log_handler): assert response.status_code == 200 assert len(handler.messages) == 1 msg = json.loads(handler.messages[0]) - assert set(msg.keys()) == constants.STANDARD_MSG_ATTRIBUTES.union({"extra_property", "tags"}) + assert set(msg.keys()) == constants.ASYNC_TASK_MSG_ATTRIBUTES.union({"extra_property", "tags"}) assert msg["extra_property"] == "extra_value" assert msg["tags"] == ["app:name"] @@ -491,7 +491,7 @@ def test_extra_property_no_props(client_and_log_handler): assert response.status_code == 200 assert len(handler.messages) == 1 msg = json.loads(handler.messages[0]) - assert set(msg.keys()) == constants.STANDARD_MSG_ATTRIBUTES.union({"extra_property", "tags"}) + assert set(msg.keys()) == constants.ASYNC_TASK_MSG_ATTRIBUTES.union({"extra_property", "tags"}) assert msg["extra_property"] == "extra_value2" assert msg["tags"] == ["app:name"] @@ -505,7 +505,7 @@ def test_exception_logged_with_stack_trace(client_and_log_handler): assert response.status_code == 200 assert len(handler.messages) == 1 msg = json.loads(handler.messages[0]) - assert set(msg.keys()) == constants.STANDARD_MSG_ATTRIBUTES.union({"exc_info", "filename"}) + assert set(msg.keys()) == constants.ASYNC_TASK_MSG_ATTRIBUTES.union({"exc_info", "filename"}) assert msg["filename"] == pathlib.Path(__file__).name, "File name for exception not logged" assert "Traceback (most recent call last):" in msg["exc_info"], "Not a stack trace" assert "RuntimeError" in msg["exc_info"], "Exception type not logged" diff --git a/tests/test_formatters_fields.py b/tests/test_formatters_fields.py index 98f69df..8d662a6 100644 --- a/tests/test_formatters_fields.py +++ b/tests/test_formatters_fields.py @@ -1,6 +1,8 @@ """Optional fields and value handling in the JSON formatters (issue #137).""" +import asyncio import json import logging +import sys import pytest @@ -91,3 +93,50 @@ def test_stack_info_is_added_when_requested(): def test_stack_info_is_omitted_by_default(): assert "stack_info" not in _format() + + +def _record(): + return logging.LogRecord("formatter-test", logging.INFO, __file__, 1, "hello", None, None) + + +@pytest.mark.skipif(sys.version_info >= (3, 12), reason="taskName is a built-in LogRecord attribute from Python 3.12") +def test_null_task_name_from_extra_is_kept_before_python_3_12(): + logger = logging.getLogger("formatter-task-name-extra-test") + records = [] + handler = logging.Handler() + handler.emit = records.append + logger.addHandler(handler) + try: + logger.warning("hello", extra={"taskName": None}) + finally: + logger.removeHandler(handler) + + output = json.loads(JSONLogFormatter().format(records[0])) + + assert "taskName" in output + assert output["taskName"] is None + + +def test_task_name_is_kept_when_set(): + assert _format(taskName="Task-7")["taskName"] == "Task-7" + + +@pytest.mark.skipif(sys.version_info < (3, 12), reason="LogRecord.taskName was added in Python 3.12") +def test_task_name_is_omitted_outside_an_asyncio_task(): + record = _record() + + assert record.taskName is None + assert "taskName" not in json.loads(JSONLogFormatter().format(record)) + + +@pytest.mark.skipif(sys.version_info < (3, 12), reason="LogRecord.taskName was added in Python 3.12") +def test_task_name_is_logged_inside_an_asyncio_task(): + async def make_record(): + return _record() + + async def main(): + return await asyncio.create_task(make_record(), name="worker-1") + + output = json.loads(JSONLogFormatter().format(asyncio.run(main()))) + + assert output["taskName"] == "worker-1"