Skip to content

exc_info_as_array is ignored for cached exception text after SocketHandler serialization #89

Closed

Description

@AryanRG

Bug

JsonFormatter(exc_info_as_array=True) emits exc_info as a string, not an array, when a received LogRecord has cached exc_text but no exc_info tuple. This is a normal shape after Python's logging.handlers.SocketHandler serializes a record: it formats the exception into exc_text and clears exc_info before transport. The formatter honors the option when the tuple is present, so the same configured output field changes JSON type across that boundary.

Reproduction

import json
import logging
import logging.handlers
import pickle
import sys

from pythonjsonlogger.json import JsonFormatter

try:
    raise ValueError("example")
except ValueError:
    record = logging.LogRecord("example", logging.ERROR, __file__, 1, "failed", (), sys.exc_info())

handler = logging.handlers.SocketHandler("localhost", 9020)
try:
    packet = handler.makePickle(record)  # Local serialization; no socket connection.
finally:
    handler.close()

received = logging.makeLogRecord(pickle.loads(packet[4:]))
result = json.loads(JsonFormatter(exc_info_as_array=True).format(received))
print(type(result["exc_info"]).__name__)  # str; expected list

This locally generated payload is decoded only for the reproduction; it does not suggest unpickling untrusted network data.

I reproduced the mismatch on current main (d036e2d6) and tested a minimal local fix that splits cached text into lines only when exc_info_as_array=True, preserving default string behavior and caller-supplied overrides. The new regression fails against base, then passes with the fix for the JSON, orjson and msgspec formatters. The local full suite passed (239 tests), along with Black, Pylint and Pyrefly. No network listener or live transport deployment was exercised.

Would you welcome a focused PR for this behavior?

AI assistance disclosure: OpenAI Codex assisted with investigation, reproduction, patch drafting, and local testing. This issue does not certify personal review or legal rights for a future PR.

Activity

  1. nhairs commented on Oct 18, 2026

    @nhairs
    Owner

    Hi @AryanRG,

    Without evidence of this affecting real use-cases I'm not sure if this is a situation I want to handle. Particularly as I would question why a modern application is encoding and transferring log records via the SocketHandler only to decode them and re-encode them.

    That said, if the change is simple enough to maintain then I would consider a PR for it. If you want to open a PR I'll check it out.

  2. added
    open for discussion
    wontfix
    on Oct 18, 2026
  3. AryanRG commented on Oct 18, 2026

    @AryanRG
    ContributorAuthor

    Thanks @nhairs, opened #95 with the small cached-text fix. I only have the local reproduction, not a production use case. Default output and caller overrides are unchanged.

  4. added
    bug
    and removed
    wontfix
    open for discussion
    on Oct 19, 2026
  5. added a commit that references this issue on Oct 20, 2026
    600e7a7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions