Skip to content

Commit 8826f75

Browse files
committed
Rename stuff in specs, add a bit of docs
1 parent 539a204 commit 8826f75

3 files changed

Lines changed: 65 additions & 41 deletions

File tree

src/daqpytools/logging/filters.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,10 @@ def _build_throttle_filter(
272272
)
273273

274274
THROTTLE_FILTER_SPEC = FilterSpec(
275-
representative_type = HandlerType.Throttle,
276-
filter_type = ThrottleFilter,
277-
factory=_build_throttle_filter
275+
alias = HandlerType.Throttle,
276+
filter_class = ThrottleFilter,
277+
factory=_build_throttle_filter,
278+
fallback_types=(HandlerType.Throttle,),
278279
)
279280

280281
FILTER_SPEC_REGISTRY: dict[HandlerType, FilterSpec] = {
@@ -294,7 +295,7 @@ def add_filter(
294295
spec = get_filter_spec(handler_type)
295296

296297
logger_filter = spec.factory(
297-
fallback_handlers if fallback_handlers is not None else spec.filter_handler_ids,
298+
fallback_handlers if fallback_handlers is not None else set(spec.fallback_types),
298299
**extras
299300
)
300301
log.addFilter(logger_filter)

src/daqpytools/logging/handlers.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
from erskafka.ERSKafkaLogHandler import ERSKafkaLogHandler
99

10-
from daqpytools.logging.exceptions import (
11-
LoggerHandlerError,
12-
ERSInitError
13-
)
10+
from daqpytools.logging.exceptions import ERSInitError, LoggerHandlerError
1411
from daqpytools.logging.filters import (
1512
HandleIDFilter,
1613
add_filter,
@@ -156,10 +153,10 @@ def _build_rich_handler(width: int | None = None, **_: Any) -> logging.Handler:
156153
return FormattedRichHandler(width=real_width)
157154

158155
RICH_HANDLER_SPEC = HandlerSpec(
159-
representative_type = HandlerType.Rich,
160-
handler_type = FormattedRichHandler,
156+
alias = HandlerType.Rich,
157+
handler_class = FormattedRichHandler,
161158
factory=_build_rich_handler,
162-
filter_handler_ids = (HandlerType.Rich,), # For HandleIDFilter
159+
fallback_types = (HandlerType.Rich,), # For HandleIDFilter
163160
)
164161

165162
def _build_stdout_handler(**_: Any) -> logging.Handler:
@@ -168,11 +165,11 @@ def _build_stdout_handler(**_: Any) -> logging.Handler:
168165
return handler
169166

170167
STDOUT_HANDLER_SPEC = HandlerSpec(
171-
representative_type = HandlerType.Lstdout,
172-
handler_type = logging.StreamHandler,
168+
alias = HandlerType.Lstdout,
169+
handler_class = logging.StreamHandler,
173170
target_stream=cast(io.IOBase, sys.stdout),
174171
factory=_build_stdout_handler,
175-
filter_handler_ids = (HandlerType.Stream, HandlerType.Lstdout),
172+
fallback_types = (HandlerType.Stream, HandlerType.Lstdout),
176173
)
177174

178175
def _build_stderr_handler(**_: Any) -> logging.Handler:
@@ -182,11 +179,11 @@ def _build_stderr_handler(**_: Any) -> logging.Handler:
182179
return handler
183180

184181
STDERR_HANDLER_SPEC = HandlerSpec(
185-
representative_type = HandlerType.Lstderr,
186-
handler_type = logging.StreamHandler,
182+
alias = HandlerType.Lstderr,
183+
handler_class = logging.StreamHandler,
187184
target_stream=cast(io.IOBase, sys.stderr),
188185
factory=_build_stderr_handler,
189-
filter_handler_ids = (HandlerType.Stream, HandlerType.Lstderr),
186+
fallback_types = (HandlerType.Stream, HandlerType.Lstderr),
190187
)
191188

192189
def _build_file_handler(path: str | None = None, **_: Any) -> logging.Handler:
@@ -199,10 +196,10 @@ def _build_file_handler(path: str | None = None, **_: Any) -> logging.Handler:
199196
return handler
200197

201198
FILE_HANDLER_SPEC = HandlerSpec(
202-
representative_type=HandlerType.File,
203-
handler_type = logging.FileHandler,
199+
alias=HandlerType.File,
200+
handler_class = logging.FileHandler,
204201
factory = _build_file_handler,
205-
filter_handler_ids=(HandlerType.File,)
202+
fallback_types=(HandlerType.File,)
206203
)
207204

208205
def _build_erskafka_handler(
@@ -224,10 +221,10 @@ def _build_erskafka_handler(
224221

225222

226223
ERSKAFKA_HANDLER_SPEC = HandlerSpec(
227-
representative_type=HandlerType.Protobufstream,
228-
handler_type = ERSKafkaLogHandler,
224+
alias=HandlerType.Protobufstream,
225+
handler_class = ERSKafkaLogHandler,
229226
factory = _build_erskafka_handler,
230-
filter_handler_ids=[HandlerType.Protobufstream],
227+
fallback_types=(HandlerType.Protobufstream,),
231228
)
232229

233230

@@ -260,7 +257,7 @@ def add_handler(
260257
if logger_or_ancestors_have_handler(
261258
log,
262259
use_parent_handlers,
263-
spec.handler_type,
260+
spec.handler_class,
264261
target_stream=spec.target_stream,
265262
):
266263
continue
@@ -269,18 +266,18 @@ def add_handler(
269266
check_parent_handlers(
270267
log,
271268
use_parent_handlers,
272-
spec.handler_type,
269+
spec.handler_class,
273270
target_stream = spec.target_stream
274271
)
275272

276273
handler = spec.factory(**extras)
277-
effective_default_case = fallback_handler if fallback_handler is not None else spec.filter_handler_ids
274+
effective_default_case = fallback_handler if fallback_handler is not None else spec.fallback_types
278275

279276
handler_ids: HandlerType | list[HandlerType]
280-
if len(spec.filter_handler_ids) == 1:
281-
handler_ids = cast(HandlerType, spec.filter_handler_ids[0])
277+
if len(spec.fallback_types) == 1:
278+
handler_ids = cast(HandlerType, spec.fallback_types[0])
282279
else:
283-
handler_ids = [cast(HandlerType, handler_id) for handler_id in spec.filter_handler_ids]
280+
handler_ids = [cast(HandlerType, handler_id) for handler_id in spec.fallback_types]
284281

285282
handler.addFilter(
286283
HandleIDFilter(
@@ -323,5 +320,5 @@ def add_handlers_from_types(
323320
continue
324321

325322
filter_spec = get_filter_spec(handler_type)
326-
if filter_spec and not logger_has_filter(log, filter_spec.filter_type):
323+
if filter_spec and not logger_has_filter(log, filter_spec.filter_class):
327324
add_filter(log, handler_type, fallback_handlers, **extras)

src/daqpytools/logging/specs.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,51 @@
1+
"""Declarative specifications used to construct logging handlers and filters.
2+
3+
This module defines immutable spec objects that describe:
4+
- how a handler or filter is built (`factory`),
5+
- how it is identified at runtime (`handler_class` / `filter_class`), and
6+
- which handler types should be used for fallback routing (`fallback_types`).
7+
"""
8+
19
import io
210
import logging
311
from collections.abc import Callable, Mapping
412
from dataclasses import dataclass
513
from typing import Any
614

7-
"""
8-
Declarative specification for building and identifying objects
9-
"""
10-
1115

1216
@dataclass(frozen=True)
1317
class HandlerSpec:
14-
representative_type: Any
15-
handler_type: type[logging.Handler]
18+
"""Specification for creating and identifying a logging handler.
19+
20+
Attributes:
21+
alias: Canonical identifier for the spec (typically a `HandlerType`).
22+
handler_class: Runtime logging class used to detect existing handlers.
23+
factory: Callable that builds the handler instance from configuration extras.
24+
fallback_types: Handler types this handler maps to for fallback behavior.
25+
target_stream: Optional stream discriminator for stream-based handlers
26+
(for example, stdout vs stderr).
27+
"""
28+
29+
alias: Any
30+
handler_class: type[logging.Handler]
1631
factory: Callable[..., logging.Handler]
17-
filter_handler_ids: tuple[Any, ...] # For HandleIDFilter
18-
target_stream: io.IOBase| None = None
32+
fallback_types: tuple[Any, ...]
33+
target_stream: io.IOBase | None = None
1934

2035

2136
@dataclass(frozen=True)
2237
class FilterSpec:
23-
representative_type: Any
24-
filter_type: type[logging.Filter]
25-
factory: Callable[[set[Any], Mapping[str, Any]], logging.Filter]
38+
"""Specification for creating and identifying a logging filter.
39+
40+
Attributes:
41+
alias: Canonical identifier for the spec (typically a `HandlerType`).
42+
filter_class: Runtime logging filter class used to detect existing filters.
43+
factory: Callable that builds the filter from fallback handlers and extras.
44+
fallback_types: Default handler types passed to the filter factory when
45+
no explicit fallback set is provided by the caller.
46+
"""
47+
48+
alias: Any
49+
filter_class: type[logging.Filter]
50+
factory: Callable[[set[Any], Mapping[str, Any]], logging.Filter]
51+
fallback_types: tuple[Any, ...] = tuple()

0 commit comments

Comments
 (0)