Setup debug file logging.
(self, file_path)
| 652 | h.setLevel(level) |
| 653 | |
| 654 | def setup_file_logging(self, file_path): |
| 655 | """Setup debug file logging.""" |
| 656 | try: |
| 657 | validated_path = self._validate_log_file_path(file_path) |
| 658 | |
| 659 | log_dir = os.path.dirname(validated_path) |
| 660 | os.makedirs(log_dir, mode=0o750, exist_ok=True) |
| 661 | |
| 662 | for h in list(self.logger.handlers): |
| 663 | if getattr(h, '_debug_file', False): |
| 664 | self.logger.removeHandler(h) |
| 665 | try: |
| 666 | h.close() |
| 667 | except (OSError, IOError) as close_error: |
| 668 | logging.warning(f'Failed to close log handler: {close_error}') |
| 669 | except Exception as unexpected_error: |
| 670 | logging.error(f'Unexpected error closing log handler: {unexpected_error}') |
| 671 | |
| 672 | # Add new file handler |
| 673 | fh = logging.FileHandler(validated_path, mode='a', encoding='utf-8') |
| 674 | fh.setLevel(logging.DEBUG) |
| 675 | fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')) |
| 676 | fh.addFilter(lambda record: record.levelno != logging.INFO) # Filter out INFO |
| 677 | fh._debug_file = True |
| 678 | self.logger.addHandler(fh) |
| 679 | self.logger.setLevel(logging.DEBUG) |
| 680 | |
| 681 | logging.info(f'Debug file logging enabled: {validated_path}') |
| 682 | return True |
| 683 | except (ValueError, OSError, IOError) as e: |
| 684 | logging.error(f'Failed to setup file logging: {e}') |
| 685 | return False |
| 686 | except Exception as e: |
| 687 | logging.error(f'Unexpected error setting up file logging: {e}') |
| 688 | return False |
| 689 | |
| 690 | def _validate_log_file_path(self, file_path): |
| 691 | """Validate and sanitize log file path to prevent security issues.""" |
no test coverage detected