Enforces custom Logger class usage over print/debugPrint with structured, categorized logging for Flutter apps. Includes BLoC patterns, error handling, and OCR processing.
Replace all `print()` and `debugPrint()` statements with a custom Logger class that provides structured, categorized, and colorized logging across your Flutter application.
Use whenever you're:
Always import the logger at the top of your files:
```dart
import 'package:pupilica_hackathon/core/helpers/logger/logger.dart';
```
```dart
Logger.debug('User tapped login button', category: LogCategory.ui);
Logger.debug('API response received',
category: LogCategory.api,
data: {'status': 200, 'data': responseData});
```
```dart
Logger.info('User successfully logged in', category: LogCategory.auth);
Logger.info('Navigation to home screen', category: LogCategory.navigation);
```
```dart
Logger.warning('API response time exceeded 5 seconds', category: LogCategory.api);
Logger.warning('Low memory detected', category: LogCategory.general);
```
```dart
try {
// Some operation
} catch (e, stackTrace) {
Logger.error('Failed to process OCR image',
category: LogCategory.ocr,
error: e,
stackTrace: stackTrace);
}
```
```dart
Logger.success('OCR processing completed successfully', category: LogCategory.ocr);
Logger.success('Data saved to database', category: LogCategory.database);
```
Choose the appropriate category for better log organization:
```dart
// ❌ Wrong
Future<FutureOr<void>> _onLoginEvent(
LoginEvent event,
Emitter<LoginState> emit,
) async {
print('Login event received'); // Don't use print
}
// ✅ Correct
Future<FutureOr<void>> _onLoginEvent(
LoginEvent event,
Emitter<LoginState> emit,
) async {
Logger.info('Login event received', category: LogCategory.auth);
}
```
```dart
// ❌ Wrong
try {
await apiCall();
} catch (e) {
print('Error: $e');
}
// ✅ Correct
try {
await apiCall();
} catch (e, stackTrace) {
Logger.error('API call failed',
category: LogCategory.api,
error: e,
stackTrace: stackTrace);
}
```
```dart
// ❌ Wrong
void _onButtonPressed() {
debugPrint('Button pressed');
setState(() {});
}
// ✅ Correct
void _onButtonPressed() {
Logger.debug('Button pressed', category: LogCategory.ui);
setState(() {});
}
```
```dart
// ✅ Correct
Future<ApiResponse> fetchData() async {
Logger.info('Fetching data from API', category: LogCategory.api);
final response = await http.get(uri);
Logger.debug('API response received',
category: LogCategory.api,
data: {
'status_code': response.statusCode,
'body_length': response.body.length
});
return ApiResponse.fromJson(jsonDecode(response.body));
}
```
```dart
final stopwatch = Stopwatch()..start();
// ... operation
stopwatch.stop();
Logger.info('Operation completed',
category: LogCategory.general,
data: {'duration_ms': stopwatch.elapsedMilliseconds});
```
```dart
// ❌ Poor
Logger.debug('test', category: LogCategory.general);
// ✅ Good
Logger.debug('User profile loaded successfully', category: LogCategory.ui);
```
```dart
Logger.debug('OCR processing started',
category: LogCategory.ocr,
data: {
'image_size': '${image.width}x${image.height}',
'processing_time': DateTime.now().millisecondsSinceEpoch
});
```
```dart
try {
// risky operation
} catch (e, stackTrace) {
Logger.error('Operation failed',
category: LogCategory.general,
error: e,
stackTrace: stackTrace,
data: {'operation': 'data_processing', 'user_id': userId});
}
```
When refactoring existing code:
1. Find all `print()` and `debugPrint()` statements
2. Determine appropriate log level (debug/info/warning/error/success)
3. Choose the right category based on context
4. Add structured data if relevant
5. Include error handling for try-catch blocks
6. Test that logs appear correctly during development
The logger provides structured output with:
Example:
```
[2024-01-15T10:30:45.123Z] Pupilica AI 🚀 SPLASH ℹ️ INFO: Splash screen initialized
[2024-01-15T10:30:45.456Z] Pupilica AI 🌐 API 🐛 DEBUG: API request sent | Data: {url: /api/login, method: POST}
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/flutter-logger-best-practices/raw