Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
8 changes: 8 additions & 0 deletions json_logging/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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')
4 changes: 4 additions & 0 deletions tests/helpers/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Constants shared by multiple tests"""
import sys

STANDARD_MSG_ATTRIBUTES = {
"written_at",
Expand All @@ -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())
3 changes: 2 additions & 1 deletion tests/test_connexion_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"


Expand All @@ -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"],
Expand Down Expand Up @@ -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"]

Expand All @@ -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"]

Expand All @@ -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"
Expand Down
49 changes: 49 additions & 0 deletions tests/test_formatters_fields.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"