Setup debug file logging.
(self, file_path)
| 555 | h.setLevel(level) |
| 556 | |
| 557 | def setup_file_logging(self, file_path): |
| 558 | """Setup debug file logging.""" |
| 559 | try: |
| 560 | validated_path = self._validate_log_file_path(file_path) |
| 561 | |
| 562 | log_dir = os.path.dirname(validated_path) |
| 563 | os.makedirs(log_dir, mode=0o750, exist_ok=True) |
| 564 | |
| 565 | for h in list(self.logger.handlers): |
| 566 | if getattr(h, '_debug_file', False): |
| 567 | self.logger.removeHandler(h) |
| 568 | try: |
| 569 | h.close() |
| 570 | except (OSError, IOError) as close_error: |
| 571 | logging.warning(f'Failed to close log handler: {close_error}') |
| 572 | except Exception as unexpected_error: |
| 573 | logging.error(f'Unexpected error closing log handler: {unexpected_error}') |
| 574 | |
| 575 | # Add new file handler |
| 576 | fh = logging.FileHandler(validated_path, mode='a', encoding='utf-8') |
| 577 | fh.setLevel(logging.DEBUG) |
| 578 | fh.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')) |
| 579 | fh.addFilter(lambda record: record.levelno != logging.INFO) # Filter out INFO |
| 580 | fh._debug_file = True |
| 581 | self.logger.addHandler(fh) |
| 582 | self.logger.setLevel(logging.DEBUG) |
| 583 | |
| 584 | logging.info(f'Debug file logging enabled: {validated_path}') |
| 585 | return True |
| 586 | except (ValueError, OSError, IOError) as e: |
| 587 | logging.error(f'Failed to setup file logging: {e}') |
| 588 | return False |
| 589 | except Exception as e: |
| 590 | logging.error(f'Unexpected error setting up file logging: {e}') |
| 591 | return False |
| 592 | |
| 593 | def _validate_log_file_path(self, file_path): |
| 594 | """Validate and sanitize log file path to prevent security issues.""" |
no test coverage detected