Feat (Logging) - Add optional rotating file logging#44
Open
WACHIFOO wants to merge 2 commits into
Open
Conversation
The controller had no logging: status and errors were only printed to the terminal, which is invisible when running the packaged executable or launching from a Raspberry Pi desktop icon, so hardware/serial failures left no trace. Add poseidon_logging.py, a small additive module that mirrors stdout and stderr (every existing print() and any traceback) into a rotating log file at logs/poseidon.log, without changing the terminal output. It is on by default and disabled with POSEIDON_LOG=0. main() calls setup_logging() at startup; no existing print() is touched. Document it in the README and ignore logs/ and *.log. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
unittest suite (stdlib, no new dependencies) covering: the POSEIDON_LOG off switch, file capture of stdout and stderr tracebacks, line buffering and blank-line preservation, idempotent setup, the no-console (frozen exe) fallback, the file-creation failure fallback, thread-safe concurrent writes, stdout/stderr console separation, the file-like interface, and the re-entrancy guard against handler-error recursion. Run from SOFTWARE/: python -m unittest test_poseidon_logging Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an optional, additive logging system to the controller software. It mirrors
everything the app already prints to the terminal (status messages and tracebacks)
into a rotating log file, without changing the existing terminal behaviour and
without modifying any of the existing
print()calls.Motivation
The controller currently has no logging — status and errors are only written to the
terminal. That terminal is invisible in the two most common deployments:
So when a pump misbehaves or the serial connection to the Arduino fails, there is no
persistent record to diagnose the problem or attach to a bug report. This is exactly
the kind of failure users hit in a wet-lab setup.
What this PR does
SOFTWARE/poseidon_logging.py, a small self-contained module.main()callssetup_logging()once at startup (a 2-line change inposeidon_main.py; no existingprint()is touched).logs/poseidon.log(rotating, ~1 MB × 3 backups) with timestamps, leveland thread name.
POSEIDON_LOG=0.logs/and*.log.Design notes
sys.stdoutandsys.stderrare wrapped so the existingprint()calls and tracebacks are captured transparently. The console output isunchanged (plain text, no prefixes;
stdout→stdout,stderr→stderr).is no console (frozen
.exe), it degrades gracefully and the app behaves exactly asbefore.
QThread); each thread buffers its ownlines, and a re-entrancy guard prevents a failing log handler from recursing.
Example
The same status/errors you see in the terminal, now also captured with context:
2026-06-07 07:28:12,986 INFO [MainThread] RUN command sent. 2026-06-07 07:28:12,990 ERROR [PumpWorker] Traceback (most recent call last): 2026-06-07 07:28:12,991 ERROR [PumpWorker] ... 2026-06-07 07:28:12,992 ERROR [PumpWorker] OSError: could not open serial port 'COM3' Testing
Pure-Python module, no hardware/PyQt/serial needed. From the
SOFTWARE/folder:
python -m unittest test_poseidon_logging 17 tests cover: the
POSEIDON_LOGoff switch, file capture of stdout and stderrtracebacks, line buffering and blank-line preservation, idempotent setup, the
no-console (frozen exe) fallback, the file-creation failure fallback, thread-safe
concurrent writes, stdout/stderr console separation, the file-like interface, and the
re-entrancy guard.
Testing notes
I don't have the pump/microscope hardware, so I haven't run this on a physical
device. The change is additive and doesn't touch the pump, serial, or GUI logic —
it only adds a log file and wraps
stdout/stderr— so runtime behaviour should beunaffected. I verified the logging module with the unit tests above and a standalone
script reproducing the app's real output patterns (status prints, a worker-thread
traceback, concurrent output). A maintainer with hardware may want a quick smoke test
to confirm startup is unaffected.
Backwards compatibility
Fully backwards compatible. With
POSEIDON_LOG=0the behaviour is identical to before;with logging on, the terminal output is unchanged and only a log file is added.
Files changed
SOFTWARE/poseidon_logging.pySOFTWARE/test_poseidon_logging.pySOFTWARE/poseidon_main.pysetup_logging()call)README.MD.gitignorelogs/and*.logChecklist
print()modifiedPOSEIDON_LOG=0python -m unittest test_poseidon_logging)Happy to adjust scope or close this if it doesn't fit the project's direction.❤️