Skip to content

Commit

Permalink
Fix ConfigLoggingUpdater not changing root logging level (#1112)
Browse files Browse the repository at this point in the history
Setting log level for root logger with method `logging.basicConfig`
during runtime is not working. Instead logging.getLogger() is used.
  • Loading branch information
ela-kotulska-frequenz authored Nov 22, 2024
2 parents 74377a4 + dcdc999 commit 8a2702f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

## Bug Fixes

<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
* Fix bug with LoggingConfigUpdater not updating root logger level.
20 changes: 14 additions & 6 deletions src/frequenz/sdk/config/_logging_config_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,25 @@ def __init__(
config_recv: The receiver to listen for configuration changes.
log_format: Use the specified format string in logs.
log_datefmt: Use the specified date/time format in logs.
Note:
The `log_format` and `log_datefmt` parameters are used in a call to
`logging.basicConfig()`. If logging has already been configured elsewhere
in the application (through a previous `basicConfig()` call), then the format
settings specified here will be ignored.
"""
super().__init__()
self._config_recv = config_recv
self._format = log_format
self._datefmt = log_datefmt

# Setup default configuration.
# This ensures logging is configured even if actor fails to start or
# if the configuration cannot be loaded.
self._current_config: LoggingConfig = LoggingConfig()

logging.basicConfig(
format=log_format,
datefmt=log_datefmt,
)
self._update_logging(self._current_config)

async def _run(self) -> None:
Expand All @@ -176,11 +185,10 @@ def _update_logging(self, config: LoggingConfig) -> None:
logging.getLogger(logger_id).setLevel(logging.NOTSET)

self._current_config = config
logging.basicConfig(
format=self._format,
level=self._current_config.root_logger.level,
datefmt=self._datefmt,
_logger.debug(
"Setting root logger level to '%s'", self._current_config.root_logger.level
)
logging.getLogger().setLevel(self._current_config.root_logger.level)

# For each logger in the new config, set the log level
for logger_id, logger_config in self._current_config.loggers.items():
Expand Down

0 comments on commit 8a2702f

Please sign in to comment.