Skip to content

fix: overhaul logging system with proper module names and crash handling#184

Closed
gmmcosta15 wants to merge 12 commits intodevfrom
bugfix/logger-manager
Closed

fix: overhaul logging system with proper module names and crash handling#184
gmmcosta15 wants to merge 12 commits intodevfrom
bugfix/logger-manager

Conversation

@gmmcosta15
Copy link
Collaborator

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).

Guilherme Costa and others added 9 commits January 29, 2026 14:03
Signed-off-by: Hugo Costa <hugo.santos.costa@gmail.com>
…sh handling

  Replace the old single-named logger pattern (logs/BlocksScreen.log) with
  standard Python logging conventions across the entire codebase.

  logger.py:
  - Add LogManager: configures the root logger once at startup; all modules
    then use logging.getLogger(__name__) with no extra setup
  - Add ThreadedFileHandler (replaces QueueListener): TimedRotatingFileHandler
    backed by a queue + daemon thread for non-blocking writes; recovers if
    log file is deleted at runtime
  - Add CrashHandler: installs sys.excepthook and threading.excepthook to
    write detailed crash logs (traceback, locals, thread list) to disk;
    uses faulthandler for C-level crashes (segfaults); daemon threads are
    exempt from os._exit(1)
  - Add StreamToLogger: redirects stderr (X11 errors, Qt warnings) to the
    logger without losing the original stream
  - Add setup_logging() entry point used by BlocksScreen.py
  - Fix: remove redundant _lock from ThreadedFileHandler.emit()
    (TimedRotatingFileHandler already holds its own lock)
  - Fix: add types.TracebackType | None annotation to _exception_hook
  - Fix: warn instead of silently return when root logger already has handlers

  Module fixes (logging.getLogger(__name__) everywhere):
  - configfile.py, lib/machine.py, lib/moonrest.py, lib/network.py,
    lib/printer.py, lib/panels/networkWindow.py,
    lib/panels/widgets/connectionPage.py
@gmmcosta15 gmmcosta15 requested a review from HugoCLSC March 3, 2026 12:27
@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 changed the title fix(logger): overhaul logging system with proper module names and crash handling fix: overhaul logging system with proper module names and crash handling Mar 3, 2026
@gmmcosta15 gmmcosta15 closed this Mar 3, 2026
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