logging
template_python.logging ¶
Loguru-based logging configuration and environment settings for template_python.
This module provides a unified interface for configuring application-level logging using loguru and Pydantic settings. It handles dynamic OpenTelemetry formatting, across the codebase and build environments.
LoggingSettings ¶
Bases: BaseSettings
Settings model for configuring the loguru logging infrastructure.
This Pydantic model loads configuration from environment variables prefixed with TEMPLATE_PYTHON__LOGGING__ and provides typed fields for controlling log output, formatting, and OpenTelemetry integration.
Example: .. code-block:: python
from template_python.logging import LoggingSettings, configure_logger
settings = LoggingSettings(enabled=True, level="DEBUG")
configure_logger(settings)
Source code in src/template_python/logging.py
model_config = SettingsConfigDict(env_prefix='TEMPLATE_PYTHON__LOGGING__', env_nested_delimiter='__') class-attribute ¶
Pydantic configuration dict dictating environment variable prefixes.
configure_logger(settings=None) ¶
Initializes the loguru logger with the provided settings or from the environment.
This function configures the global loguru logger instance based on the provided LoggingSettings. It handles enabling/disabling the logger, managing sinks, and injecting the appropriate formatter (including OpenTelemetry).
Example: .. code-block:: python
from template_python.logging import configure_logger, LoggingSettings
configure_logger(LoggingSettings(level="DEBUG"))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings | LoggingSettings | None | An optional instance of | None |
Returns:
| Type | Description |
|---|---|
None | None |
Raises:
| Type | Description |
|---|---|
ImportError | If OpenTelemetry formatting is explicitly enabled but the package is not installed. |