đ Lesson 12: Logging & Debugging
Why Logging Matters
When your app is running in production, you can't just `print()` everywhere. Logging gives you eyes on your application's behavior, even when things go wrong silently.
đ
Debug Remotely
See what happened without running the app locally.
đ
Monitor Health
Track performance and error rates over time.
đĄī¸
Audit Trail
Know exactly what actions occurred and when.
đ§
Filter Noise
Show only critical issues in production.
Today's Project: "Professional Logger"
We'll build a production-ready logging system:
- đ Configurable log levels
- đ File-based persistent logs
- đī¸ Hierarchical loggers for modules
- đ Dependency injection pattern
- đ Log analysis features
Step 1
Understanding Log Levels
đ Log Level Hierarchy
Drag the slider to set the minimum log level. Messages below this threshold are filtered out.
đ [FINEST] Detailed trace data
đ [FINER] Less detailed trace
đ [FINE] Debug information
âī¸ [CONFIG] Configuration loaded
âšī¸ [INFO] Application started
â ī¸ [WARNING] Low disk space
â [SEVERE] Connection failed
đ¨ [SHOUT] Critical system failure!
Step 2
Live Logging Simulator
đĨī¸ Real-Time Log Console
Simulate different application events and watch logs appear:
Logs will appear here...
đ INFO: 0
â ī¸ WARN: 0
â ERR: 0
đ DEBUG: 0
Step 3
Writing Logs to Files
đ File Logger Configuration
Configure where and how logs are persisted:
đ Generated Logger Setup:
final logFile = File('./logs/app_2026_05_25.txt');
logger.onRecord.listen((record) {
final msg = '[${record.time}] ${record.level.name}: ${record.message}';
logFile.writeAsStringSync('$msg\n', mode: FileMode.append);
if (record.level >= Level.WARNING) print(msg);
});
Step 4
Dependency Injection with Loggers
đ Pass Loggers via Constructor
See how to inject loggers into your service classes:
đ main()
Creates Logger
â Logger â
đ SearchCommand
Logger logger
â Logger â
đ GetArticleCommand
Logger logger
đĄ Implementation Pattern:
class SearchCommand {
final Logger logger;
SearchCommand({required this.logger});
Future<void> execute(String query) async {
logger.info('Searching for: $query');
// ... implementation
}
}
đ
Build: Production Logger System
đ Logger Configuration
đ§ Service with Injected Logger
đŽ Main Application
đ¤ Simulated Log Output
Click "Run Simulation" to see log output
đŽ Final Challenge: Logging Master Quiz
đ Score: 0/5
⥠Streak: 0
âą Time: 0s
đ Logs: 100%