|
1 | | -"""Queue-backed logging configuration used during early startup. |
2 | | -
|
3 | | -Implements: ROADMAP STAB-1 (the rotating-file ``quill.log`` and the |
4 | | -queue listener that drains log records off the UI thread so a |
5 | | -flooded log channel never blocks the wx main thread). The handler |
6 | | -list installed here is what :mod:`quill.stability.crash_report` reads |
7 | | -when the user saves a diagnostic bundle. |
8 | | -""" |
9 | | - |
10 | | -from __future__ import annotations |
11 | | - |
12 | | -import logging |
13 | | -import logging.handlers |
14 | | -import queue |
15 | | -from pathlib import Path |
16 | | - |
17 | | - |
18 | | -def configure_logging(log_dir: Path) -> logging.handlers.QueueListener: |
19 | | - log_dir.mkdir(parents=True, exist_ok=True) |
20 | | - |
21 | | - log_queue: queue.Queue[logging.LogRecord] = queue.Queue(maxsize=10_000) |
22 | | - queue_handler = logging.handlers.QueueHandler(log_queue) |
23 | | - |
24 | | - root = logging.getLogger() |
25 | | - root.setLevel(logging.INFO) |
26 | | - root.handlers.clear() |
27 | | - root.addHandler(queue_handler) |
28 | | - |
29 | | - file_handler = logging.handlers.RotatingFileHandler( |
30 | | - log_dir / "quill.log", |
31 | | - maxBytes=5_000_000, |
32 | | - backupCount=5, |
33 | | - encoding="utf-8", |
34 | | - ) |
35 | | - file_handler.setFormatter( |
36 | | - logging.Formatter("%(asctime)s %(levelname)s %(threadName)s %(name)s: %(message)s") |
37 | | - ) |
38 | | - |
39 | | - listener = logging.handlers.QueueListener( |
40 | | - log_queue, |
41 | | - file_handler, |
42 | | - respect_handler_level=True, |
43 | | - ) |
44 | | - listener.start() |
45 | | - return listener |
| 1 | +"""Queue-backed logging configuration used during early startup. |
| 2 | +
|
| 3 | +Implements: ROADMAP STAB-1 (the rotating-file ``quill.log`` and the |
| 4 | +queue listener that drains log records off the UI thread so a |
| 5 | +flooded log channel never blocks the wx main thread). The handler |
| 6 | +list installed here is what :mod:`quill.stability.crash_report` reads |
| 7 | +when the user saves a diagnostic bundle. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import logging |
| 13 | +import logging.handlers |
| 14 | +import queue |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | + |
| 18 | +def configure_logging(log_dir: Path) -> logging.handlers.QueueListener: |
| 19 | + log_dir.mkdir(parents=True, exist_ok=True) |
| 20 | + |
| 21 | + log_queue: queue.Queue[logging.LogRecord] = queue.Queue(maxsize=10_000) |
| 22 | + queue_handler = logging.handlers.QueueHandler(log_queue) |
| 23 | + |
| 24 | + root = logging.getLogger() |
| 25 | + root.setLevel(logging.INFO) |
| 26 | + root.handlers.clear() |
| 27 | + root.addHandler(queue_handler) |
| 28 | + |
| 29 | + file_handler = logging.handlers.RotatingFileHandler( |
| 30 | + log_dir / "quill.log", |
| 31 | + maxBytes=5_000_000, |
| 32 | + backupCount=5, |
| 33 | + encoding="utf-8", |
| 34 | + ) |
| 35 | + file_handler.setFormatter( |
| 36 | + logging.Formatter("%(asctime)s %(levelname)s %(threadName)s %(name)s: %(message)s") |
| 37 | + ) |
| 38 | + |
| 39 | + listener = logging.handlers.QueueListener( |
| 40 | + log_queue, |
| 41 | + file_handler, |
| 42 | + respect_handler_level=True, |
| 43 | + ) |
| 44 | + listener.start() |
| 45 | + return listener |
| 46 | + |
| 47 | + |
| 48 | +def relocate_log(listener: logging.handlers.QueueListener, log_dir: Path) -> None: |
| 49 | + """Move the active ``quill.log`` to *log_dir* at runtime (quill-radio #5). |
| 50 | +
|
| 51 | + Swaps the listener's rotating-file handler for one writing into *log_dir*, |
| 52 | + so a user who changes the log-folder preference sees new records land there |
| 53 | + without a restart. The old handler is closed. A no-op (leaving the current |
| 54 | + handler in place) if the new directory can't be created, so a bad path |
| 55 | + never leaves logging broken. |
| 56 | + """ |
| 57 | + try: |
| 58 | + log_dir.mkdir(parents=True, exist_ok=True) |
| 59 | + new_handler = logging.handlers.RotatingFileHandler( |
| 60 | + log_dir / "quill.log", |
| 61 | + maxBytes=5_000_000, |
| 62 | + backupCount=5, |
| 63 | + encoding="utf-8", |
| 64 | + ) |
| 65 | + except OSError: |
| 66 | + logging.getLogger(__name__).warning( |
| 67 | + "Could not relocate the log to %s; keeping the current location.", log_dir |
| 68 | + ) |
| 69 | + return |
| 70 | + new_handler.setFormatter( |
| 71 | + logging.Formatter("%(asctime)s %(levelname)s %(threadName)s %(name)s: %(message)s") |
| 72 | + ) |
| 73 | + old_handlers = list(listener.handlers) |
| 74 | + listener.stop() |
| 75 | + listener.handlers = (new_handler,) |
| 76 | + listener.start() |
| 77 | + for handler in old_handlers: |
| 78 | + try: |
| 79 | + handler.close() |
| 80 | + except OSError: |
| 81 | + pass |
0 commit comments