Skip to content

Commit 6d00f12

Browse files
committed
Add docs on migration
1 parent 59af999 commit 6d00f12

4 files changed

Lines changed: 130 additions & 0 deletions

File tree

docs/how-to/upgrade-package.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This documentation is split into two sections depending on your role:
1919
| [How to route messages](./how-to/route-messages.md) | Direct records to specific handlers using HandlerType and LogHandlerConf |
2020
| [How to add handlers at runtime](./how-to/add-handlers-at-runtime.md) | Attach handlers after logger creation; pass kwargs |
2121
| [How to configure ERS](./how-to/configure-ers.md) | Attach and use ERS handlers |
22+
| [How to upgrade an existing package](./how-to/upgrade-package.md) | Migration checklist for adopting daqpytools logging in existing codebases |
2223
| [Best practices](./how-to/best-practices.md) | Recommended patterns for structuring logging in your application |
2324
| [Troubleshooting](./reference/troubleshooting.md) | Common symptoms, causes, and fixes |
2425
| [API reference](https://dune-daq.github.io/daqpytools/APIref) | Auto-generated kwargs, types, and defaults for all public APIs (redirects to MKDocs website) |

docs_dev/readme_toplevel.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Furthermore, a separate API reference set that includes Auto-generated kwargs, t
2121
| [How to route messages](./user/how-to/route-messages.md) | Direct records to specific handlers using HandlerType and LogHandlerConf |
2222
| [How to add handlers at runtime](./user/how-to/add-handlers-at-runtime.md) | Attach handlers after logger creation; pass kwargs |
2323
| [How to configure ERS](./user/how-to/configure-ers.md) | Attach and use ERS handlers |
24+
| [How to upgrade an existing package](./user/how-to/upgrade-package.md) | Migration checklist for adopting daqpytools logging in existing codebases |
2425
| [Best practices](./user/how-to/best-practices.md) | Recommended patterns for structuring logging in your application |
2526
| [Troubleshooting](./user/reference/troubleshooting.md) | Common symptoms, causes, and fixes |
2627

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ nav:
1515
- Route messages: user/how-to/route-messages.md
1616
- Add handlers at runtime: user/how-to/add-handlers-at-runtime.md
1717
- Configure ERS: user/how-to/configure-ers.md
18+
- Upgrade an existing package: user/how-to/upgrade-package.md
1819
- Best practices: user/how-to/best-practices.md
1920
- Troubleshooting: user/reference/troubleshooting.md
2021
- Developer Documentation:

0 commit comments

Comments
 (0)