|
| 1 | +# How to add daqpytools logging to an existing Python package |
| 2 | + |
| 3 | +This page is a practical migration checklist for packages that already use Python logging and want to standardise on `daqpytools.logging`. |
| 4 | + |
| 5 | +For design rationale and architecture, read [Best practices](./best-practices.md) and [Concepts & explanation](../explanation.md). |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Migration checklist |
| 10 | + |
| 11 | +## 1) Choose a pseudo-root logger name |
| 12 | + |
| 13 | +Pick one stable package-level prefix, for example: |
| 14 | + |
| 15 | +- `drunc` |
| 16 | +- `mypkg` |
| 17 | + |
| 18 | +All application/module loggers should live under this namespace: |
| 19 | + |
| 20 | +- `mypkg.cli` |
| 21 | +- `mypkg.worker` |
| 22 | +- `mypkg.worker.io` |
| 23 | + |
| 24 | +## 2) Initialise the pseudo-root once, early |
| 25 | + |
| 26 | +Call `setup_root_logger` at process startup (entrypoint/main), before creating child loggers. |
| 27 | + |
| 28 | +```python |
| 29 | +from daqpytools.logging import setup_root_logger |
| 30 | + |
| 31 | +setup_root_logger("mypkg", log_level="INFO") |
| 32 | +``` |
| 33 | + |
| 34 | +The pseudo-root should be a clean inheritance anchor and should not be configured with arbitrary ad-hoc handlers. |
| 35 | + |
| 36 | +## 3) Create a package helper to enforce naming/inheritance |
| 37 | + |
| 38 | +Define a small helper so all code consistently gets namespaced loggers. |
| 39 | + |
| 40 | +```python |
| 41 | +from daqpytools.logging import get_daq_logger |
| 42 | + |
| 43 | +PSEUDO_ROOT = "mypkg" |
| 44 | + |
| 45 | + |
| 46 | +def get_logger(name: str, **kwargs): |
| 47 | + full_name = f"{PSEUDO_ROOT}.{name}" if name else PSEUDO_ROOT |
| 48 | + return get_daq_logger(logger_name=full_name, **kwargs) |
| 49 | +``` |
| 50 | + |
| 51 | +This avoids accidental logger-name drift and makes traceability much better. |
| 52 | + |
| 53 | +## 4) Configure handlers at parent/package boundaries |
| 54 | + |
| 55 | +Attach default handlers where they make sense architecturally (often package/module entry points), not everywhere. |
| 56 | + |
| 57 | +```python |
| 58 | +from mypkg.logging_utils import get_logger |
| 59 | + |
| 60 | +# Example package-level logger with default rich output |
| 61 | +get_logger("utils", rich_handler=True, log_level="INFO") |
| 62 | +``` |
| 63 | + |
| 64 | +Child loggers then inherit handlers naturally. |
| 65 | + |
| 66 | +## 5) Migrate module files to child loggers |
| 67 | + |
| 68 | +In each module, request a child logger and use it directly. |
| 69 | + |
| 70 | +```python |
| 71 | +from mypkg.logging_utils import get_logger |
| 72 | + |
| 73 | +log = get_logger("worker.io") |
| 74 | + |
| 75 | + |
| 76 | +def run() -> None: |
| 77 | + log.info("Worker started") |
| 78 | +``` |
| 79 | + |
| 80 | +Prefer inheritance over repeatedly reconfiguring handlers. |
| 81 | + |
| 82 | +## 6) Replace old logging patterns safely |
| 83 | + |
| 84 | +During migration, remove or phase out patterns that interfere with shared configuration: |
| 85 | + |
| 86 | +- `logging.basicConfig(...)` |
| 87 | +- direct root-logger mutation |
| 88 | +- ad-hoc per-file handler wiring that duplicates inherited setup |
| 89 | + |
| 90 | +Incremental migration is fine; it does not need to be a flag day. |
| 91 | + |
| 92 | +## 7) Add ERS support (optional) |
| 93 | + |
| 94 | +If your application uses ERS routing: |
| 95 | + |
| 96 | +1. Create/reuse your package logger |
| 97 | +2. Call `setup_daq_ers_logger(...)` |
| 98 | +3. Ensure ERS environment variables are set before logger initialisation |
| 99 | + |
| 100 | +```python |
| 101 | +from daqpytools.logging import get_daq_logger, setup_daq_ers_logger |
| 102 | + |
| 103 | +log = get_daq_logger("mypkg.app", rich_handler=True) |
| 104 | +setup_daq_ers_logger(log, ers_kafka_session="my-session", ers_app_name="mypkg") |
| 105 | +``` |
| 106 | + |
| 107 | +See [How to configure ERS](./configure-ers.md) for details. |
| 108 | + |
| 109 | +--- |
| 110 | + |
| 111 | +## Suggested rollout plan |
| 112 | + |
| 113 | +1. Add the helper and pseudo-root setup. |
| 114 | +2. Convert one subsystem to namespaced child loggers. |
| 115 | +3. Validate output/routing and inheritance. |
| 116 | +4. Convert remaining modules. |
| 117 | +5. Remove legacy logging configuration paths. |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +## Quick validation checklist |
| 122 | + |
| 123 | +- Pseudo-root initialised once at startup |
| 124 | +- No `logging.basicConfig` in runtime paths |
| 125 | +- Package helper used consistently |
| 126 | +- Parent handlers configured once, child loggers mostly inherited |
| 127 | +- Optional ERS setup performed after env vars are available |
0 commit comments