LLD Hub
Problems·SRP: Fix the Overloaded Logger
SOLIDL2 · Easy

SRP: Fix the Overloaded Logger

Problem

You are given a Logger class that does too much: it formats log messages, filters by level, writes to console, writes to a file, AND sends critical logs to a database. This violates the Single Responsibility Principle. Refactor it so each class has exactly one reason to change.

Requirements

  • LogFormatter — formats a log entry (timestamp + level + message)
  • LogFilter — decides whether a log should pass based on minimum level
  • ConsoleHandler — writes formatted logs to console
  • FileHandler — writes formatted logs to a file path
  • DatabaseHandler — persists CRITICAL logs to DB
  • Logger — orchestrates the above, but owns none of the logic itself
  • Demonstrate: changing log format should only require editing LogFormatter

Constraints

  • Logger class must not contain any formatting, filtering, or IO logic
  • Each handler should be independently testable
✓ Saved