Set the filename parameter for Logging.FileHandler(). Creates parent directory if it does not exist. .. warning:: This is an experimental API.
(self, fname: str)
| 606 | return formatter |
| 607 | |
| 608 | def set_log_path(self, fname: str) -> None: |
| 609 | """Set the filename parameter for Logging.FileHandler(). |
| 610 | |
| 611 | Creates parent directory if it does not exist. |
| 612 | |
| 613 | .. warning:: |
| 614 | This is an experimental API. |
| 615 | """ |
| 616 | fpath = Path(fname) |
| 617 | |
| 618 | if not fpath.is_absolute(): |
| 619 | fpath = self._config.rootpath / fpath |
| 620 | |
| 621 | if not fpath.parent.exists(): |
| 622 | fpath.parent.mkdir(exist_ok=True, parents=True) |
| 623 | |
| 624 | stream = fpath.open(mode="w", encoding="UTF-8") |
| 625 | if sys.version_info >= (3, 7): |
| 626 | old_stream = self.log_file_handler.setStream(stream) |
| 627 | else: |
| 628 | old_stream = self.log_file_handler.stream |
| 629 | self.log_file_handler.acquire() |
| 630 | try: |
| 631 | self.log_file_handler.flush() |
| 632 | self.log_file_handler.stream = stream |
| 633 | finally: |
| 634 | self.log_file_handler.release() |
| 635 | if old_stream: |
| 636 | # https://github.com/python/typeshed/pull/5663 |
| 637 | old_stream.close() # type:ignore[attr-defined] |
| 638 | |
| 639 | def _log_cli_enabled(self): |
| 640 | """Return whether live logging is enabled.""" |