Skip to main content

Getting Ready: Logging Framework

Editorial

Why This Problem Matters

Every production system in the world logs. When a payment fails at Stripe, an engineer opens the logs. When a request times out at Netflix, the first diagnostic step is checking the logs. When a Kubernetes pod crashes, the cluster's logging pipeline captures the dying breath of the container before it's recycled. Logging is the nervous system of software — invisible when things go well, indispensable when they don't.

But logging frameworks are more than print() statements. A real logging framework must solve several non-trivial engineering problems simultaneously:

  • Level filtering — in production, you want ERROR and WARN messages but not DEBUG chatter. In development, you want everything. The framework must filter messages by severity without changing application code.
  • Multiple output destinations (sinks) — a single log message might need to go to the console for developer convenience, a file for persistent storage, and a remote database or service like Datadog, Splunk, or the ELK stack for centralized monitoring.
  • Extensibility — when your company adopts a new monitoring tool next quarter, adding a new log sink should not require modifying the existing logger code. This is the Open/Closed Principle in action.
  • Many loggers, each configured once — loggers are used everywhere: controllers, services, repositories, utilities. Different parts of a program want different levels (verbose for the part you are debugging, quiet everywhere else), so a program has many loggers, and each is configured in one place, at start-up, rather than scattered through the code.

Real-world logging frameworks that solve these problems include:

  • Python's logging module — built into the standard library, uses handlers (sinks), formatters, and level filtering
  • Java's Log4j / SLF4J / Logback — the de facto standard in the Java ecosystem, with appenders for console, file, database, and network destinations
  • Winston (Node.js) — transport-based architecture where each transport is an output destination
  • Serilog (.NET) — structured logging with sink-based output routing

All of these share the same foundational design: loggers that filter by level and dispatch to pluggable sinks — and many loggers per program, handed out by name (Python's logging.getLogger("payments"), SLF4J's LoggerFactory.getLogger(...)), each with its own level and sinks. This is precisely what we'll build.

In LLD interviews, the Logging Framework is a favorite because it naturally exercises two important design patterns — Observer and Chain of Responsibility — and a classic trap, the Singleton Logger, in a system that every developer intuitively understands. Interviewers love it because it tests whether you can design for extensibility (adding new sinks) without overengineering.

What You'll Learn

In this design problem, you will:

  • Answer the Singleton question — why a Logger should not be a Singleton: a program has many loggers, each with its own level and sinks, made once at start-up and passed to the code that logs through them
  • Apply the Chain of Responsibility Pattern — route log messages through a chain of level-based filters, where each handler decides whether to process or pass the message along
  • Apply the Observer Pattern — decouple the logger from its output destinations so that multiple sinks receive and process log messages independently
  • Design for the Open/Closed Principle — structure the system so adding a new log sink (e.g., Slack notifications, Datadog integration) requires zero modification to existing code
  • Model the system with a UML class diagram — identify classes, relationships, and the role of each design pattern in the structure
  • Implement in Python and Java — write complete, compilable logging framework code in both languages
  • Test edge cases — handle concurrent logging, empty sink lists, level boundaries, and sink failures gracefully

Prerequisites

Before starting this problem, make sure you're comfortable with:

  • OOP Fundamentals — classes, encapsulation, inheritance, and polymorphism (see Classes and Objects — Blueprint vs Instance and Polymorphism — Compile-Time vs Runtime). The Logging Framework relies heavily on polymorphism for interchangeable sinks
  • OOP Relationships — especially Association and Composition (see Association — Objects That Know Each Other). The Logger associates with multiple LogSink objects
  • Interfaces vs Abstract Classes — you'll define a LogSink interface that concrete sinks implement (see Interfaces vs Abstract Classes)
  • UML Class Diagrams — how to read class boxes, interfaces, and relationship arrows (see Class Diagrams — Structure of Your System)
  • Open/Closed Principle — the 'O' in SOLID (see O — Open/Closed Principle). This problem is one of the clearest demonstrations of OCP — new sinks are added by implementing an interface, not by modifying the logger
  • Singleton Pattern — what it guarantees and what it costs (see Singleton Pattern). This problem shows why a logger should not be one
  • Observer Pattern — one-to-many notification (see Observer Pattern — Event-Driven Communication). The logger notifies all registered sinks when a message passes the level filter
  • Chain of Responsibility Pattern — sequential delegation through a chain of handlers (see Chain of Responsibility — Passing the Request). Used for level-based filtering

If you've completed the Parking Lot, Tic Tac Toe, Snake and Ladder, and LRU Cache problems, you're well-prepared. This problem introduces a new challenge: designing infrastructure-level software rather than domain-specific systems.

Problem Scope

We will design: The core logging framework — a Logger class that accepts log messages at different severity levels, filters them based on a configured minimum level, and dispatches matching messages to one or more pluggable sinks (Console, File, Database). Each Logger stays a plain object — a program makes as many as it needs, each with its own level and sinks — and adding new sinks requires no changes to existing code.

We will NOT design: Log aggregation pipelines (like the ELK stack), distributed tracing, log rotation/archival policies, structured logging (JSON output), network transport protocols, or monitoring dashboards. This is a Low Level Design (object-oriented) exercise focused on the framework's internal architecture, not a System Design (infrastructure) problem.

Difficulty: Easy — The Logging Framework has a small number of classes (6-8) and straightforward relationships. What makes it an excellent learning exercise is that it naturally showcases design patterns working together in a cohesive system — and one deliberately left out. The extensibility story is clean and compelling.