Comprehensive logging service for the application with multiple outputs and environment-aware formatting.
The logging layer provides:
LoggingService- Core logging managementLogOutputtemplates - Plug-and-play outputs (Console, File, Crashlytics)LogFormatter- JSON and pretty-print formattingLogProviders- Riverpod providers for dependency injection
Main service for handling all application logs.
Location: lib/core/logging/logging_service.dart
- Multiple Levels: debug, info, warning, error.
- Structured Data: Supports passing a
Map<String, dynamic>as context. - Environment Aware:
- Dev: Pretty printing to console.
- Prod: JSON formatting for easier ingestion by log aggregators.
- Togglable: Respects
ENABLE_LOGGINGflag from environment config.
final logger = ref.read(loggingServiceProvider);
// Basic message
logger.info('User logged in');
// With context
logger.debug('Fetching data', context: {'id': 123, 'retry': true});
// With errors (automatically captures stack trace if not provided)
try {
// ...
} catch (e, stack) {
logger.error('Operation failed', error: e, stackTrace: stack);
}An optional Firebase Crashlytics Log Output template.
Location: lib/core/logging/crashlytics_output_template.dart
- Run:
flutter pub add firebase_crashlytics - Follow the instructions at the top of the file to uncomment the logic.
- Add to
LoggingService:if (_enableRemoteLogging) { outputs.add(CrashlyticsOutput()); }
This template is opt-in and not enabled by default.
- Automatically filters logs to only send
Level.warningandLevel.errorto Firebase. - Maps
Level.erroras "fatal" errors in the Crashlytics dashboard.
- Configuration System - How to toggle logging via
.env - Common Patterns - Error handling and logging best practices