Skip to content

fix: PlainFileHandler never rotates, regression of #2538 - #4824

Merged
Vasilije1990 merged 2 commits into
topoteretes:mainfrom
BrightMine35:fix/plainfilehandler-rotation
Aug 31, 2026
Merged

fix: PlainFileHandler never rotates, regression of #2538#4824
Vasilije1990 merged 2 commits into
topoteretes:mainfrom
BrightMine35:fix/plainfilehandler-rotation

Conversation

@BrightMine35

Copy link
Copy Markdown
Contributor

Summary

  • PlainFileHandler.emit() fully overrides RotatingFileHandler.emit() (needed for its structlog dict-message formatting) and never calls shouldRollover()/doRollover(). maxBytes/backupCount are accepted and stored, and the class docstring claims automatic rotation, but nothing ever triggers it.
  • This is a regression of Logging writes unbounded files to site-packages/logs/ without rotation #2538: that issue was closed by switching this class's base from FileHandler to RotatingFileHandler, but the pre-existing emit() override was never updated to actually call the rotation check, so the original unbounded-growth symptom recurs under a different shape (one huge file instead of many small ones).
  • Confirmed locally: an install with LOG_FILE_NAME pinned to one path grew that file to 297MB with zero .1/.2 backups ever appearing.
  • Fix adds a direct stream-size check rather than calling self.shouldRollover(record) — its stock implementation estimates the pending message's length via self.format(record), which assumes a plain-string message. Since this handler exists specifically for structlog dict-shaped records (record.msg is often a dict), running the stock formatter just to estimate a length risks raising on the very record the check is meant to protect.

Fixes #4823

Test plan

  • python -m py_compile cognee/shared/logging_utils.py
  • Isolated test: 200 real structlog dict-message logger.info({"event": ..., "logger": ...}) calls against a throwaway file with maxBytes=2000, backupCount=2 — rotation fires correctly, .1/.2 backups created, current file stays bounded, zero exceptions raised by the new check, zero malformed/truncated lines in any retained file.
  • Not run against the full test suite in this environment — happy to adjust based on CI/maintainer feedback.

PlainFileHandler.emit() fully overrides RotatingFileHandler.emit()
(needed for structlog-aware dict-message formatting) and never calls
shouldRollover()/doRollover(). maxBytes/backupCount are accepted and
stored by the inherited __init__, and the class docstring claims
automatic rotation, but nothing ever triggers it -- confirmed the
string "rollover" doesn't appear anywhere in the file before this fix.

topoteretes#2538 was closed by switching this class's base from FileHandler to
RotatingFileHandler and passing maxBytes/backupCount through, but the
pre-existing emit() override was never updated to actually call the
rotation check, so the original unbounded-growth symptom recurs
(confirmed locally: one log file to 297MB with zero .1/.2 backups
ever created, on an install with LOG_FILE_NAME pinned to a single
path).

Adds a direct stream-size check instead of calling
self.shouldRollover(record) -- its stock implementation estimates the
pending message's length via self.format(record), which assumes a
plain-string message. Since this handler exists specifically for
structlog dict-shaped records, running the stock formatter just to
estimate a length risks raising on the very record the check is
meant to protect.

Tested in isolation: 200 real structlog dict-message calls against a
throwaway file (maxBytes=2000, backupCount=2) -- rotation fires,
stays bounded, zero exceptions, zero malformed lines.

Fixes topoteretes#4823
@Vasilije1990
Vasilije1990 changed the base branch from main to dev August 29, 2026 06:18
@Vasilije1990

Copy link
Copy Markdown
Contributor

@BrightMine35 please resolve conflicts

…local git merge-tree and a real test merge (both clean, zero conflicts) that this branch has no actual content conflict with upstream/main; the API's 'dirty' mergeable_state appears to be a stale cache rather than a real conflict.
@Vasilije1990
Vasilije1990 merged commit 8b86f86 into topoteretes:main Aug 31, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression of #2538: PlainFileHandler still never rotates — emit() override never calls shouldRollover()/doRollover()

2 participants