Skip to content

Commit e3c13cc

Browse files
committed
Add better docstrings
1 parent 405346b commit e3c13cc

File tree

3 files changed

+143
-21
lines changed

3 files changed

+143
-21
lines changed

src/daqpytools/logging/filters.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,23 @@ def _build_throttle_filter(
267267
time_limit: int = 30,
268268
**extras: object,
269269
) -> logging.Filter:
270-
"""Build a throttle filter from extras."""
270+
"""Build a throttle filter.
271+
272+
Args:
273+
fallback_handlers: Handler types used as fallback routing context.
274+
initial_treshold: Number of first occurrences to emit before applying
275+
suppression logic.
276+
time_limit: Throttle time window in seconds.
277+
**extras: Additional forwarded keyword arguments. Ignored by this
278+
factory.
279+
280+
Returns:
281+
A configured ``ThrottleFilter`` instance.
282+
283+
Notes:
284+
The keyword name is currently ``initial_treshold`` to match the
285+
existing function signature.
286+
"""
271287
del extras
272288
return ThrottleFilter(
273289
fallback_handlers=fallback_handlers,
@@ -287,7 +303,15 @@ def _build_throttle_filter(
287303
}
288304

289305
def get_filter_spec(handler_types: HandlerType) -> FilterSpec | None:
290-
"""Return the filter specification for a handler type."""
306+
"""Return the filter specification for a handler type.
307+
308+
Args:
309+
handler_types: Filter-capable ``HandlerType`` alias.
310+
311+
Returns:
312+
The matching ``FilterSpec`` if present in ``FILTER_SPEC_REGISTRY``;
313+
otherwise ``None``.
314+
"""
291315
return FILTER_SPEC_REGISTRY.get(handler_types)
292316

293317
def add_filter(
@@ -296,7 +320,20 @@ def add_filter(
296320
fallback_handlers : set[HandlerType]| None,
297321
**extras: object,
298322
) -> None:
299-
"""Add a logger filter according to the spec."""
323+
"""Add a logger filter resolved from ``FILTER_SPEC_REGISTRY``.
324+
325+
Args:
326+
log: Logger receiving the filter instance.
327+
handler_type: Filter-capable ``HandlerType`` to resolve.
328+
fallback_handlers: Explicit fallback handler set passed to the filter
329+
factory. If ``None``, the filter spec ``fallback_types`` are used.
330+
**extras: Additional keyword arguments forwarded to the resolved filter
331+
factory. These values typically come from
332+
``get_daq_logger(..., **extras)``.
333+
334+
Returns:
335+
None.
336+
"""
300337
spec = get_filter_spec(handler_type)
301338

302339
effective_fallback_handlers = (
@@ -315,7 +352,19 @@ def add_throttle_filter(
315352
log: logging.Logger,
316353
fallback_handlers: set[HandlerType] | None = None,
317354
) -> None:
318-
"""Add the Throttle filter to the logger."""
355+
"""Add the throttle filter to a logger.
356+
357+
This is a convenience wrapper over ``add_filter`` for
358+
``HandlerType.Throttle``.
359+
360+
Args:
361+
log: Logger receiving the throttle filter.
362+
fallback_handlers: Optional fallback handler set used by throttle
363+
routing. If omitted, registry defaults are used.
364+
365+
Returns:
366+
None.
367+
"""
319368
add_filter(
320369
log,
321370
HandlerType.Throttle,

src/daqpytools/logging/handlers.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,19 @@ def logger_or_ancestors_have_handler(
148148
#### Handlers ####
149149

150150
def _build_rich_handler(width: int | None = None, **_: object) -> logging.Handler:
151-
"""Building the rich handler with any extras."""
151+
"""Build the rich console handler.
152+
153+
This factory is invoked from handler resolution in ``get_daq_logger`` and
154+
receives forwarded ``**extras``.
155+
156+
Args:
157+
width: Optional console width used by ``FormattedRichHandler``.
158+
If ``None``, terminal width is auto-detected via ``get_width``.
159+
**_: Additional forwarded keyword arguments. Ignored by this factory.
160+
161+
Returns:
162+
The configured rich logging handler.
163+
"""
152164
real_width = width if width is not None else get_width()
153165
return FormattedRichHandler(width=real_width)
154166

@@ -160,6 +172,14 @@ def _build_rich_handler(width: int | None = None, **_: object) -> logging.Handle
160172
)
161173

162174
def _build_stdout_handler(**_: object) -> logging.Handler:
175+
"""Build a stdout stream handler.
176+
177+
Args:
178+
**_: Additional forwarded keyword arguments. Ignored by this factory.
179+
180+
Returns:
181+
A ``logging.StreamHandler`` writing to ``sys.stdout``.
182+
"""
163183
handler = logging.StreamHandler(sys.stdout)
164184
handler.setFormatter(LoggingFormatter())
165185
return handler
@@ -173,6 +193,15 @@ def _build_stdout_handler(**_: object) -> logging.Handler:
173193
)
174194

175195
def _build_stderr_handler(**_: object) -> logging.Handler:
196+
"""Build a stderr stream handler.
197+
198+
Args:
199+
**_: Additional forwarded keyword arguments. Ignored by this factory.
200+
201+
Returns:
202+
A ``logging.StreamHandler`` writing to ``sys.stderr`` with
203+
``ERROR`` level.
204+
"""
176205
handler = logging.StreamHandler(sys.stderr)
177206
handler.setFormatter(LoggingFormatter())
178207
handler.setLevel(logging.ERROR)
@@ -187,6 +216,19 @@ def _build_stderr_handler(**_: object) -> logging.Handler:
187216
)
188217

189218
def _build_file_handler(path: str | None = None, **_: object) -> logging.Handler:
219+
"""Build a file handler.
220+
221+
Args:
222+
path: Path to the output log file. This is typically forwarded from
223+
``get_daq_logger(..., file_handler_path=...)`` as ``path``.
224+
**_: Additional forwarded keyword arguments. Ignored by this factory.
225+
226+
Returns:
227+
A configured ``logging.FileHandler``.
228+
229+
Raises:
230+
ValueError: If ``path`` is not provided.
231+
"""
190232
if not path:
191233
err_msg = "path is required for file handler"
192234
raise ValueError(err_msg)
@@ -208,6 +250,21 @@ def _build_erskafka_handler(
208250
address : str = "monkafka.cern.ch:30092",
209251
ers_app_name : str | None = None,
210252
**_: object) -> logging.Handler:
253+
"""Build an ERS Kafka handler.
254+
255+
Args:
256+
session_name: ERS session name used by the Kafka handler.
257+
topic: Kafka topic for ERS log messages.
258+
address: Kafka broker address in ``host:port`` format.
259+
ers_app_name: Optional ERS application name associated with messages.
260+
**_: Additional forwarded keyword arguments. Ignored by this factory.
261+
262+
Returns:
263+
A configured ``ERSKafkaLogHandler`` instance.
264+
265+
Raises:
266+
ERSInitError: If the handler cannot be initialized.
267+
"""
211268

212269
try:
213270
return ERSKafkaLogHandler(

src/daqpytools/logging/logger.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,44 @@ def get_daq_logger(
7171
throttle: bool = False,
7272
**extras: object
7373
) -> logging.Logger:
74-
"""C'tor for the default logging instances.
74+
"""Create or reuse a configured DAQ logger.
75+
76+
Handler/filter installation is driven by selected flags and resolved through
77+
the handler/filter registries. Additional keyword arguments are forwarded to
78+
the underlying factory functions.
7579
7680
Args:
77-
logger_name (str): Name of the logger.
78-
log_level (int | str): Log level for the logger.
79-
use_parent_handlers (bool): Whether to use parent handlers.
80-
rich_handler (bool): Whether to add a rich handler.
81-
file_handler_path (str | None): Path to the file handler log file. If None, no
82-
file handler is added.
83-
stream_handlers (bool): Whether to add both stdout and stderr stream handlers.
84-
ers_kafka_session (str | None): ERS session name used to add an ERS
85-
protobuf handler. If None, no ERS protobuf handler is added.
86-
throttle (bool): Whether to add the throttle filter or not. Note, does not mean
87-
outputs are filtered by default! See ThrottleFilter for details.
88-
**extras (object): Extra keyword arguments forwarded to handler builders.
81+
logger_name: Name of the logger to create or retrieve.
82+
log_level: Logging level for the logger and its non-stderr handlers.
83+
use_parent_handlers: If ``True``, logger propagation remains enabled.
84+
rich_handler: Enable ``HandlerType.Rich``.
85+
file_handler_path: Optional file path enabling ``HandlerType.File``.
86+
stream_handlers: Enable ``HandlerType.Stream`` (stdout + stderr specs).
87+
ers_kafka_session: Optional ERS session enabling
88+
``HandlerType.Protobufstream``.
89+
throttle: Enable ``HandlerType.Throttle`` filter installation.
90+
**extras: Additional keyword arguments forwarded to handler/filter
91+
factories via ``add_handlers_from_types(..., **extras)``.
92+
93+
Common forwarded kwargs include:
94+
- ``width`` -> ``_build_rich_handler``
95+
- ``path`` -> ``_build_file_handler`` (internally mapped from
96+
``file_handler_path``)
97+
- ``session_name`` -> ``_build_erskafka_handler`` (internally mapped
98+
from ``ers_kafka_session``)
99+
- ``topic``, ``address``, ``ers_app_name`` ->
100+
``_build_erskafka_handler``
101+
- ``initial_treshold``, ``time_limit`` ->
102+
``_build_throttle_filter``
103+
104+
Unsupported kwargs may be ignored by factories that accept ``**_``.
89105
90106
Returns:
91-
logging.Logger: Configured logger instance.
107+
Configured ``logging.Logger`` instance.
92108
93109
Raises:
94-
LoggerSetupError: If the configuration is invalid.
95-
110+
LoggerSetupError: If a logger with the same name already exists but
111+
with a conflicting handler configuration.
96112
"""
97113
rich_traceback_install(show_locals=True, width=get_width())
98114

0 commit comments

Comments
 (0)