Skip to content

Commit 4937680

Browse files
Fix ConfigLoggingUpdater not changing root logging level
Setting log level with method `logging.basicConfig` during runtime is not working. Instead logging.getLogger() is used.
1 parent 53792f2 commit 4937680

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
## Bug Fixes
1616

17-
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
17+
* Fix bug with LoggingConfigUpdater not updating root logger level.

src/frequenz/sdk/config/_logging_config_updater.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,16 +141,24 @@ def __init__(
141141
config_recv: The receiver to listen for configuration changes.
142142
log_format: Use the specified format string in logs.
143143
log_datefmt: Use the specified date/time format in logs.
144+
145+
Note:
146+
log_format and log_datefmt are passed to the `logging.basicConfig`
147+
function. If any call to `logging.basicConfig` is made before this
148+
actor is created, then this actor will not override it.
144149
"""
145150
super().__init__()
146151
self._config_recv = config_recv
147-
self._format = log_format
148-
self._datefmt = log_datefmt
149152

150153
# Setup default configuration.
151154
# This ensures logging is configured even if actor fails to start or
152155
# if the configuration cannot be loaded.
153156
self._current_config: LoggingConfig = LoggingConfig()
157+
158+
logging.basicConfig(
159+
format=log_format,
160+
datefmt=log_datefmt,
161+
)
154162
self._update_logging(self._current_config)
155163

156164
async def _run(self) -> None:
@@ -176,11 +184,10 @@ def _update_logging(self, config: LoggingConfig) -> None:
176184
logging.getLogger(logger_id).setLevel(logging.NOTSET)
177185

178186
self._current_config = config
179-
logging.basicConfig(
180-
format=self._format,
181-
level=self._current_config.root_logger.level,
182-
datefmt=self._datefmt,
187+
_logger.debug(
188+
"Setting root logger level to '%s'", self._current_config.root_logger.level
183189
)
190+
logging.getLogger().setLevel(self._current_config.root_logger.level)
184191

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

0 commit comments

Comments
 (0)