📝 Lesson 12: Logging & Debugging

⏱ 25-35 min 📊 Intermediate 📝 Production Ready 🎓 Final Lesson 🔗 Official Source
📝

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.

ALLFINESTFINERFINECONFIGINFOWARNINGSEVERESHOUT
Current: INFO
🔍 [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%

Loading question...

← Previous Lesson
Final Project →