Skip to content

feat(logger): overhaul logging system with module-aware names and c…#186

Merged
HugoCLSC merged 7 commits intodevfrom
bugfix/logger-clean
Mar 3, 2026
Merged

feat(logger): overhaul logging system with module-aware names and c…#186
HugoCLSC merged 7 commits intodevfrom
bugfix/logger-clean

Conversation

@gmmcosta15
Copy link
Collaborator

@gmmcosta15 gmmcosta15 commented Mar 3, 2026

Description

Select the type:

  • Feature
  • Bug fix
  • Code refactor
  • Documentation

Replaces the broken single-named logger pattern (logging.getLogger("logs/BlocksScreen.log")) and bare logging.error() / logging.debug() calls.

  • Add LogManager: configures the root logger once at startup; all modules then call logging.getLogger(name) with no extra setup required.
  • Add ThreadedFileHandler: wraps TimedRotatingFileHandler with an internal queue and daemon worker thread for non-blocking file writes;
    auto-recovers if the log file is deleted at runtime.
  • Add CrashHandler: installs sys.excepthook and threading.excepthook to write detailed crash reports (full traceback, local variables per frame, active thread list) to disk on unhandled exceptions; uses faulthandler for C-level crashes (segfaults); daemon threads are exempt from os._exit(1).
  • Add StreamToLogger: redirects stderr (X11 errors, Qt warnings) into the logging system without losing the original stream.
  • Add setup_logging() public entry point, called once in BlocksScreen.py at startup.
  • Fix ThreadedFileHandler: remove redundant _lock — TimedRotatingFileHandler already holds its own internal lock.
  • Fix CrashHandler._exception_hook: add types.TracebackType | None type annotation.
  • Fix LogManager.setup(): log a WARNING instead of silently returning when the root logger already has handlers.
  • Fix all modules to use logging.getLogger(name): configfile.py, lib/machine.py, lib/moonrest.py, lib/network.py, lib/printer.py,
    lib/panels/networkWindow.py, lib/panels/widgets/connectionPage.py.
  • Fix NetworkControlWindow.init: unconditionally pass parent to super().init().
  • Re-enable NetworkControlWindow instantiation and signal connections in MainWindow (was commented out).
  • Fix MainWindow.closeEvent: remove recursive self.close() call before super().

Motivation

The old logging setup used logging.getLogger("logs/BlocksScreen.log") as the logger name in every module. This caused all log output to show
logs/BlocksScreen.log as the source instead of the actual module name, making it impossible to trace which module emitted a given log line.
Additionally, several modules called logging.error() / logging.debug() directly, routing through the root logger and showing root in output.
This PR establishes the standard Python pattern: one setup_logging() call at startup configures the root logger, and every module independently
calls logging.getLogger(name) — zero coupling to the logging infrastructure. The result is accurate module names in every log line (e.g. lib.panels.networkWindow, lib.moonrakerComm) which makes debugging and production log analysis significantly faster.
The CrashHandler addition addresses production incidents where the application exits silently on unhandled exceptions or segfaults with no recoverable information in the log files.

Screenshots

[INFO] | 2026-03-03 12:04:27,445 | logger | MainThread : Logging initialized
[INFO] | 2026-03-03 12:04:27,445 | main | MainThread : ============ BlocksScreen Initializing ============
[INFO] | 2026-03-03 12:04:27,570 | lib.moonrakerComm | MainThread : Websocket object initialized
[DEBUG] | 2026-03-03 12:04:27,993 | lib.panels.widgets.connectionPage | MainThread : [ConnectionWindowPanel] text_update: 1
[INFO] | 2026-03-03 12:04:28,029 | lib.moonrakerComm | websocket.run_forever : Connection to websocket achieved

Future work

  • Add unit tests for LogManager, ThreadedFileHandler, and CrashHandler (currently no test coverage on the logger module).

…ash handling

  - Replace AsyncFileHandler with ThreadedFileHandler (queue + background
    thread wrapping TimedRotatingFileHandler; "async" was a misnomer)
  - Add LogManager singleton: configures root logger, optional
    StreamToLogger capture of stderr/stdout, handler deduplication
  - Add CrashHandler: installs sys.excepthook, threading.excepthook, and
    faulthandler for C-level crashes; supports exit_on_crash flag
  - Expose setup_logging() and get_logger() as the public API
  - Suppress noisy third-party loggers (urllib3, websocket, PIL) to WARNING
  - Fix all modules to use logging.getLogger(__name__) instead of the old
    named-file pattern (logs/BlocksScreen.log):
      BlocksScreen.py, configfile.py, lib/machine.py, lib/moonrest.py,
      lib/printer.py, lib/panels/mainWindow.py,
      lib/panels/widgets/connectionPage.py
  - Replace manual logger-iteration loop in MainWindow.closeEvent with
    LogManager.shutdown(); remove erroneous recursive self.close() call
  - Fix create_hotspot password param: str | None = None, raise ValueError
    instead of silently accepting empty/hardcoded default (bandit B107)
  - Replace all bare except/pass blocks with typed exception handling that
    writes to sys.__stderr__ when the logger itself may be unavailable
    (bandit B110)
  EOF
  )"
@gmmcosta15 gmmcosta15 requested a review from HugoCLSC March 3, 2026 15:49
@gmmcosta15 gmmcosta15 self-assigned this Mar 3, 2026
@gmmcosta15 gmmcosta15 added enhancement New feature or request. Refactor Enhancing code's readability, maintainability, and extensibility while addressing technical debt. labels Mar 3, 2026
@gmmcosta15 gmmcosta15 marked this pull request as ready for review March 3, 2026 15:55
@gmmcosta15 gmmcosta15 marked this pull request as draft March 3, 2026 16:06
@gmmcosta15 gmmcosta15 marked this pull request as ready for review March 3, 2026 16:28
@gmmcosta15 gmmcosta15 marked this pull request as draft March 3, 2026 16:44
@gmmcosta15 gmmcosta15 marked this pull request as ready for review March 3, 2026 16:46
@HugoCLSC HugoCLSC merged commit 1720ae0 into dev Mar 3, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request. Refactor Enhancing code's readability, maintainability, and extensibility while addressing technical debt.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants