From 4ae5fc177e9d5e4ce748001875ea66c92cbc2516 Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Wed, 20 Nov 2024 15:00:24 +0800 Subject: [PATCH] better control on log files --- analyze.py | 49 ++++++++++------------ config/portfolio_config.json | 7 ++++ src/utils/logger.py | 81 +++++++++++++++++++++++++++--------- 3 files changed, 89 insertions(+), 48 deletions(-) diff --git a/analyze.py b/analyze.py index 590128f..2e3de79 100644 --- a/analyze.py +++ b/analyze.py @@ -9,44 +9,37 @@ from src.utils.config_manager import ConfigManager def main(): - # Setup console for rich output - console = Console() - - try: - # Setup logging - logger = setup_logger() - logger.info("Starting ESP3D Portfolio analysis") + # Load configuration + config_path = Path("config/portfolio_config.json") + if not config_path.exists(): + print(f"ERROR: Configuration file not found at {config_path}") + return - # Load configuration - config_path = Path("config/portfolio_config.json") - if not config_path.exists(): - console.print("[red]Configuration file not found![/red]") - sys.exit(1) - + try: config_manager = ConfigManager(config_path) - # Check for GitHub token + # Setup logging with config + logger = setup_logger(config_manager) + logger.info("Starting ESP3D Portfolio analysis") + + # Vérifier que le token GitHub est configuré github_token = os.getenv('GITHUB_TOKEN') if not github_token: - console.print("[red]GitHub token not found! Please set GITHUB_TOKEN environment variable.[/red]") - sys.exit(1) - + logger.error("GITHUB_TOKEN environment variable is not set") + return + # Initialize analyzer analyzer = PortfolioAnalyzer(github_token, config_manager) - # Run analysis with progress tracking - with console.status("[bold green]Analyzing repositories...") as status: - analyzer.run_analysis() - - console.print("[green]Analysis completed successfully![/green]") + # Run analysis + analyzer.run_analysis() + + logger.info("Analysis completed successfully") - except KeyboardInterrupt: - console.print("\n[yellow]Analysis interrupted by user[/yellow]") - sys.exit(0) except Exception as e: - console.print(f"[red]Error during analysis: {str(e)}[/red]") - logger.exception("Error during analysis") - sys.exit(1) + print(f"Error during analysis: {str(e)}") + if logger: + logger.exception("Error during analysis") if __name__ == "__main__": main() \ No newline at end of file diff --git a/config/portfolio_config.json b/config/portfolio_config.json index 2951599..b5a7c31 100644 --- a/config/portfolio_config.json +++ b/config/portfolio_config.json @@ -223,5 +223,12 @@ "enabled": true, "directory": ".cache", "max_age": 3600 + }, + "logging": { + "enabled": true, + "level": "INFO", + "max_files": 7, + "file_logging": false, + "console_logging": true } } diff --git a/src/utils/logger.py b/src/utils/logger.py index 843a150..7bdb32e 100644 --- a/src/utils/logger.py +++ b/src/utils/logger.py @@ -3,34 +3,75 @@ from rich.logging import RichHandler from datetime import datetime -def setup_logger(log_dir="logs"): - # Create logs directory if it doesn't exist - Path(log_dir).mkdir(exist_ok=True) +def setup_logger(config_manager, log_dir="logs"): + """Configure logging based on configuration settings""" + log_config = config_manager.config.get('logging', {}) - # Configure logging - log_filename = f"{log_dir}/portfolio_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log" + # Si les logs sont complètement désactivés + if not log_config.get('enabled', True): + logging.getLogger().setLevel(logging.ERROR) + return logging.getLogger("portfolio") - # Formateur détaillé - formatter = logging.Formatter( - '%(asctime)s - %(name)s - %(levelname)s - %(message)s' - ) + # Déterminer le niveau de log + log_level = getattr(logging, log_config.get('level', 'INFO').upper()) + + # Configurer les handlers + handlers = [] - # Handler pour le fichier - file_handler = logging.FileHandler(log_filename) - file_handler.setLevel(logging.DEBUG) - file_handler.setFormatter(formatter) + # Handler pour la console + if log_config.get('console_logging', True): + console_handler = RichHandler(rich_tracebacks=True) + console_handler.setLevel(log_level) + handlers.append(console_handler) - # Handler pour la console avec Rich - console_handler = RichHandler(rich_tracebacks=True) - console_handler.setLevel(logging.INFO) + # Handler pour les fichiers + if log_config.get('file_logging', False): + # Créer le répertoire des logs si nécessaire + Path(log_dir).mkdir(exist_ok=True) + + # Nettoyer les anciens fichiers de log + max_files = log_config.get('max_files', 7) + cleanup_old_logs(log_dir, max_files) + + # Créer le nouveau fichier de log + log_filename = f"{log_dir}/portfolio_{datetime.now().strftime('%Y%m%d_%H%M%S')}.log" + file_handler = logging.FileHandler(log_filename) + file_handler.setLevel(log_level) + file_handler.setFormatter( + logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + ) + handlers.append(file_handler) # Configuration de base logging.basicConfig( - level=logging.DEBUG, # Niveau global à DEBUG - handlers=[console_handler, file_handler] + level=log_level, + handlers=handlers ) logger = logging.getLogger("portfolio") - logger.setLevel(logging.DEBUG) + logger.setLevel(log_level) + + if not log_config.get('file_logging', False): + logger.info("File logging is disabled") + + return logger + +def cleanup_old_logs(log_dir: str, max_files: int): + """Nettoyer les anciens fichiers de log""" + log_path = Path(log_dir) + if not log_path.exists(): + return + + # Liste tous les fichiers de log triés par date de modification + log_files = sorted( + log_path.glob("portfolio_*.log"), + key=lambda x: x.stat().st_mtime, + reverse=True + ) - return logger \ No newline at end of file + # Supprimer les fichiers excédentaires + for old_log in log_files[max_files:]: + try: + old_log.unlink() + except Exception: + pass \ No newline at end of file