import 'package:flutter/foundation.dart'; import 'package:geosector_app/core/services/api_service.dart'; /// Service de logging centralisé qui désactive automatiquement les logs en production class LoggerService { static LoggerService? _instance; static bool? _isProduction; // Singleton static LoggerService get instance { _instance ??= LoggerService._internal(); return _instance!; } LoggerService._internal(); /// Détermine si on est en production static bool get isProduction { if (_isProduction != null) return _isProduction!; try { // Vérifier si ApiService est initialisé final env = ApiService.instance.getCurrentEnvironment(); _isProduction = env == 'PROD'; } catch (e) { // Si ApiService n'est pas encore initialisé, utiliser kReleaseMode _isProduction = kReleaseMode; } return _isProduction!; } /// Réinitialiser le cache de l'environnement (utile pour les tests) static void resetEnvironmentCache() { _isProduction = null; } /// Log simple (remplace debugPrint) static void log(String message, {String? prefix}) { if (!isProduction) { if (prefix != null) { debugPrint('$prefix $message'); } else { debugPrint(message); } } } /// Log d'information static void info(String message) { if (!isProduction) { debugPrint('ℹ️ $message'); } } /// Log de succès static void success(String message) { if (!isProduction) { debugPrint('✅ $message'); } } /// Log d'avertissement static void warning(String message) { if (!isProduction) { debugPrint('⚠️ $message'); } } /// Log d'erreur (toujours affiché même en production pour le debugging) static void error(String message, [dynamic error, StackTrace? stackTrace]) { // Les erreurs sont toujours loggées, même en production debugPrint('❌ $message'); if (error != null) { debugPrint(' Error: $error'); } if (stackTrace != null && !isProduction) { // La stack trace n'est affichée qu'en DEV/REC debugPrint(' Stack trace:\n$stackTrace'); } } /// Log de debug (avec emoji personnalisé) static void debug(String message, {String emoji = '🔧'}) { if (!isProduction) { debugPrint('$emoji $message'); } } /// Log de requête API static void api(String message) { if (!isProduction) { debugPrint('🔗 $message'); } } /// Log de base de données static void database(String message) { if (!isProduction) { debugPrint('💾 $message'); } } /// Log de navigation static void navigation(String message) { if (!isProduction) { debugPrint('🧭 $message'); } } /// Log de performance/timing static void performance(String message) { if (!isProduction) { debugPrint('⏱️ $message'); } } /// Log conditionnel basé sur un flag custom static void conditional(String message, {required bool condition}) { if (!isProduction && condition) { debugPrint(message); } } /// Groupe de logs (pour regrouper visuellement des logs liés) static void group(String title, List messages) { if (!isProduction) { debugPrint('┌─ $title'); for (int i = 0; i < messages.length; i++) { if (i == messages.length - 1) { debugPrint('└─ ${messages[i]}'); } else { debugPrint('├─ ${messages[i]}'); } } } } /// Log d'objet JSON (formaté) static void json(String label, Map data) { if (!isProduction) { debugPrint('📋 $label:'); data.forEach((key, value) { debugPrint(' $key: $value'); }); } } } /// Extension pour faciliter l'utilisation extension LoggerExtension on String { void log({String? prefix}) => LoggerService.log(this, prefix: prefix); void logInfo() => LoggerService.info(this); void logSuccess() => LoggerService.success(this); void logWarning() => LoggerService.warning(this); void logError([dynamic error, StackTrace? stackTrace]) => LoggerService.error(this, error, stackTrace); void logDebug({String emoji = '🔧'}) => LoggerService.debug(this, emoji: emoji); void logApi() => LoggerService.api(this); void logDatabase() => LoggerService.database(this); void logNavigation() => LoggerService.navigation(this); void logPerformance() => LoggerService.performance(this); }