Enforces custom Logger class usage instead of print/debugPrint in Flutter apps. Provides structured, categorized, and colorized logging with different levels (debug, info, warning, error, success) and categories (splash, navigation, api, database, ui, auth, ocr, general).
This skill enforces the use of a custom Logger class for all logging operations in Flutter applications, replacing `print()` and `debugPrint()` statements with structured, categorized logging.
At the top of every Dart file that requires logging:
```dart
import 'package:pupilica_hackathon/core/helpers/logger/logger.dart';
```
**NEVER use:**
**ALWAYS use:**
**Debug (`Logger.debug()`):**
**Info (`Logger.info()`):**
**Warning (`Logger.warning()`):**
**Error (`Logger.error()`):**
**Success (`Logger.success()`):**
Always specify the `category` parameter:
When logging, include relevant contextual data:
```dart
Logger.debug('API response received',
category: LogCategory.api,
data: {
'status': response.statusCode,
'body_length': response.body.length,
'endpoint': '/api/users'
});
```
Always include error and stack trace:
```dart
try {
// risky operation
} catch (e, stackTrace) {
Logger.error('Operation failed',
category: LogCategory.general,
error: e,
stackTrace: stackTrace,
data: {'context': 'additional context'});
}
```
Log execution times for performance monitoring:
```dart
final stopwatch = Stopwatch()..start();
// ... operation
stopwatch.stop();
Logger.info('Operation completed',
category: LogCategory.general,
data: {'duration_ms': stopwatch.elapsedMilliseconds});
```
**Before (incorrect):**
```dart
Future<FutureOr<void>> _onLoginEvent(
LoginEvent event,
Emitter<LoginState> emit,
) async {
print('Login event received');
// logic
}
```
**After (correct):**
```dart
Future<FutureOr<void>> _onLoginEvent(
LoginEvent event,
Emitter<LoginState> emit,
) async {
Logger.info('Login event received', category: LogCategory.auth);
// logic
}
```
**Before (incorrect):**
```dart
try {
await apiCall();
} catch (e) {
print('Error: $e');
}
```
**After (correct):**
```dart
try {
await apiCall();
} catch (e, stackTrace) {
Logger.error('API call failed',
category: LogCategory.api,
error: e,
stackTrace: stackTrace);
}
```
**Before (incorrect):**
```dart
void _onButtonPressed() {
debugPrint('Button pressed');
setState(() {});
}
```
**After (correct):**
```dart
void _onButtonPressed() {
Logger.debug('Button pressed', category: LogCategory.ui);
setState(() {});
}
```
When refactoring existing code:
1. Search for all `print()` and `debugPrint()` statements
2. Determine appropriate log level (debug/info/warning/error/success)
3. Choose correct category based on context
4. Add structured data via `data` parameter when relevant
5. Include error and stackTrace for all exception handlers
6. Add Logger import at top of file
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-81bfgw/raw