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