renames function, can now take the name of the logger (defaults to root logger)

This commit is contained in:
Mose Müller 2025-03-27 11:43:15 +01:00
parent 23f051d6f1
commit 0565c82448
2 changed files with 37 additions and 13 deletions

View File

@ -191,19 +191,43 @@ def setup_logging() -> None:
logging.config.dictConfig(LOGGING_CONFIG)
def configure_root_logger_with_pydase_formatter(
level: int = logging.INFO, stream: TextIO | None = None
def configure_logging_with_pydase_formatter(
name: str | None = None, level: int = logging.INFO, stream: TextIO | None = None
) -> None:
"""Configures root logger with pydase formatting. Log level and stream are
configurable.
"""Configure a logger with the pydase `DefaultFormatter`.
This sets up a `StreamHandler` with the custom `DefaultFormatter`, which includes
timestamp, log level with color (if supported), logger name, function, and line
number. It can be used to configure the root logger or any named logger.
Args:
level: Logging level of this logger. Defaults to logging.INFO.
stream: Stream used in the logging handler. If None is passed, std.err is used.
Defaults to None.
"""
name: The name of the logger to configure. If None, the root logger is used.
level: The logging level to set on the logger (e.g., logging.DEBUG,
logging.INFO). Defaults to logging.INFO.
stream: The output stream for the log messages (e.g., sys.stdout or sys.stderr).
If None, defaults to sys.stderr.
root_logger = logging.getLogger()
Example:
Configure logging in your service:
```python
import sys
from pydase.utils.logging import configure_logging_with_pydase_formatter
configure_logging_with_pydase_formatter(
name="my_service", # Use the package/module name or None for the root logger
level=logging.DEBUG, # Set the desired logging level (defaults to INFO)
stream=sys.stdout # Set the output stream (stderr by default)
)
```
Notes:
- This function adds a new handler each time it's called.
Use carefully to avoid duplicate logs.
- Colors are enabled if the stream supports TTY (e.g., in terminal).
""" # noqa: E501
logger = logging.getLogger(name=name)
handler = logging.StreamHandler(stream=stream)
formatter = DefaultFormatter(
fmt="%(asctime)s.%(msecs)03d | %(levelprefix)s | "
@ -211,5 +235,5 @@ def configure_root_logger_with_pydase_formatter(
datefmt="%Y-%m-%d %H:%M:%S",
)
handler.setFormatter(formatter)
root_logger.addHandler(handler)
root_logger.setLevel(level)
logger.addHandler(handler)
logger.setLevel(level)

View File

@ -1,7 +1,7 @@
import logging
import pytest
from pydase.utils.logging import configure_root_logger_with_pydase_formatter
from pydase.utils.logging import configure_logging_with_pydase_formatter
def test_log_error(caplog: pytest.LogCaptureFixture) -> None:
@ -78,7 +78,7 @@ def test_before_configuring_root_logger(caplog: pytest.LogCaptureFixture) -> Non
def test_configure_root_logger(caplog: pytest.LogCaptureFixture) -> None:
configure_root_logger_with_pydase_formatter()
configure_logging_with_pydase_formatter()
logger = logging.getLogger(__name__)
logger.info("Hello world")