Skip to content

Commit

Permalink
better control on log files
Browse files Browse the repository at this point in the history
  • Loading branch information
luc-github committed Nov 20, 2024
1 parent 3dc554a commit 4ae5fc1
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 48 deletions.
49 changes: 21 additions & 28 deletions analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
7 changes: 7 additions & 0 deletions config/portfolio_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
81 changes: 61 additions & 20 deletions src/utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
# Supprimer les fichiers excédentaires
for old_log in log_files[max_files:]:
try:
old_log.unlink()
except Exception:
pass

0 comments on commit 4ae5fc1

Please sign in to comment.