|
11 | 11 | import marshmallow.validate |
12 | 12 |
|
13 | 13 | from ..actor import Actor |
14 | | -from ._manager import ConfigManager |
| 14 | +from ._manager import ConfigManager, wait_for_first |
15 | 15 |
|
16 | 16 | _logger = logging.getLogger(__name__) |
17 | 17 |
|
@@ -143,23 +143,45 @@ def __init__( |
143 | 143 | datefmt=log_datefmt, |
144 | 144 | level=logging.INFO, |
145 | 145 | ) |
146 | | - self._update_logging(self._current_config) |
| 146 | + _logger.info("Applying initial default logging configuration...") |
| 147 | + self._reconfigure(self._current_config) |
147 | 148 |
|
148 | 149 | async def _run(self) -> None: |
149 | 150 | """Listen for configuration changes and update logging.""" |
150 | | - async for new_config in self._config_receiver: |
151 | | - match new_config: |
152 | | - case None: |
153 | | - # When we receive None, we want to reset the logging configuration |
154 | | - # to the default |
155 | | - self._update_logging(LoggingConfig()) |
156 | | - case LoggingConfig(): |
157 | | - self._update_logging(new_config) |
158 | | - case Exception(): |
159 | | - # We ignore errors and just keep the old configuration |
160 | | - pass |
161 | | - case unexpected: |
162 | | - assert_never(unexpected) |
| 151 | + self._reconfigure( |
| 152 | + await wait_for_first( |
| 153 | + self._config_receiver, receiver_name=str(self), allow_none=True |
| 154 | + ) |
| 155 | + ) |
| 156 | + async for config_update in self._config_receiver: |
| 157 | + self._reconfigure(config_update) |
| 158 | + |
| 159 | + def _reconfigure(self, config_update: LoggingConfig | Exception | None) -> None: |
| 160 | + """Update the logging configuration. |
| 161 | +
|
| 162 | + Args: |
| 163 | + config_update: The new configuration, or an exception if there was an error |
| 164 | + parsing the configuration, or `None` if the configuration was unset. |
| 165 | + """ |
| 166 | + match config_update: |
| 167 | + case LoggingConfig(): |
| 168 | + _logger.info( |
| 169 | + "New configuration received, updating logging configuration." |
| 170 | + ) |
| 171 | + self._update_logging(config_update) |
| 172 | + case None: |
| 173 | + _logger.info( |
| 174 | + "Configuration was unset, resetting to the default " |
| 175 | + "logging configuration." |
| 176 | + ) |
| 177 | + self._update_logging(LoggingConfig()) |
| 178 | + case Exception(): |
| 179 | + _logger.info( |
| 180 | + "New configuration has errors, keeping the old logging " |
| 181 | + "configuration." |
| 182 | + ) |
| 183 | + case unexpected: |
| 184 | + assert_never(unexpected) |
163 | 185 |
|
164 | 186 | def _update_logging(self, config: LoggingConfig) -> None: |
165 | 187 | """Configure the logging level.""" |
|
0 commit comments