Setup debug file logging.
(self, file_path)
| 680 | h.setLevel(level) |
| 681 | |
| 682 | def setup_file_logging(self, file_path): |
| 683 | """Setup debug file logging.""" |
| 684 | try: |
| 685 | validated_path = self._validate_log_file_path(file_path) |
| 686 | |
| 687 | log_dir = os.path.dirname(validated_path) |
| 688 | os.makedirs(log_dir, mode=0o750, exist_ok=True) |
| 689 | |
| 690 | for h in list(self.logger.handlers): |
| 691 | if getattr(h, '_debug_file', False): |
| 692 | self.logger.removeHandler(h) |
| 693 | try: |
| 694 | h.close() |
| 695 | except (OSError, IOError) as close_error: |
| 696 | logging.warning(f'Failed to close log handler: {close_error}') |
| 697 | except Exception as unexpected_error: |
| 698 | logging.error(f'Unexpected error closing log handler: {unexpected_error}') |
| 699 | |
| 700 | # Add new file handler |
| 701 | fh = logging.FileHandler(validated_path, mode='a', encoding='utf-8') |
| 702 | fh.setLevel(logging.DEBUG) |
| 703 | fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')) |
| 704 | fh.addFilter(lambda record: record.levelno != logging.INFO) # Filter out INFO |
| 705 | fh._debug_file = True |
| 706 | self.logger.addHandler(fh) |
| 707 | self.logger.setLevel(logging.DEBUG) |
| 708 | |
| 709 | logging.info(f'Debug file logging enabled: {validated_path}') |
| 710 | return True |
| 711 | except (ValueError, OSError, IOError) as e: |
| 712 | logging.error(f'Failed to setup file logging: {e}') |
| 713 | return False |
| 714 | except Exception as e: |
| 715 | logging.error(f'Unexpected error setting up file logging: {e}') |
| 716 | return False |
| 717 | |
| 718 | def _validate_log_file_path(self, file_path): |
| 719 | """Validate and sanitize log file path to prevent security issues.""" |
no test coverage detected