Design a Logging System

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2025-03-31

Problem Statement

Design a logging system for applications to record messages at different levels (INFO, DEBUG, ERROR, etc.) with support for filtering, formatting, and storage. Focus is on OOP design, modularity, and extensibility.


Functional Requirements


1.Log Message
•Applications can log messages using an API: log(level, message).
•Supported log levels: INFO, DEBUG, WARN, ERROR, FATAL.
2.Log Formatting
•Include timestamp, log level, message, and optional metadata (e.g., module, thread ID).
•Support customizable format templates.
3.Log Storage / Output
•Write logs to different outputs:
•Console
•File
•Remote server (optional)
4.Filtering and Querying
•Allow filtering logs by level, timestamp range, or component/module.
•Optionally support querying recent N logs.
5.Optional Features
•Asynchronous logging to avoid blocking main application.
•Log rotation (by size or time).
•Thread-safe logging for multi-threaded applications.


Non-Functional Requirements


•Thread Safety: Handle concurrent logging from multiple threads.
•Performance: Minimal impact on application performance.
•Extensibility: Easy to add new log levels or outputs.
•Reliability: Ensure logs are not lost in case of crashes (buffering or flushing).


Edge Cases & Constraints


•High-frequency logging → use buffering or asynchronous writing.
•File size limit reached → rotate logs automatically.
•Logging while output destination is unavailable → handle errors gracefully.


Design Hints


•Key Classes / Interfaces:
•Logger → main API to log messages.
•LogLevel → enum for INFO, DEBUG, ERROR, etc.
•LogMessage → encapsulates timestamp, level, message, and metadata.
•LogFormatter → formats log messages.
•LogHandler / Appender → handles output to console, file, or remote server.
•LoggerManager → manages multiple logger instances, configurations.
•Data Structures:
•Queue → for buffering log messages before writing asynchronously.
•Map<Level, Handler> → to route logs to specific handlers.
•Design Patterns:
•Singleton Pattern → for centralized logger instance.
•Strategy Pattern → for pluggable formatting and output strategies.
•Observer Pattern (Optional) → for monitoring log events.


Example Flow


1.Application calls Logger.log(INFO, "User logged in").
2.Logger creates a LogMessage object with timestamp and metadata.
3.LogFormatter formats the message.
4.LogHandler writes the formatted message to console/file/remote server.
5.If asynchronous, LogMessage is queued and processed by a background thread.


Evaluation Areas


•Clean OOP design and separation of concerns.
•Thread-safe and efficient logging mechanism.
•Flexible design for formatting, filtering, and multiple outputs.
•Handling edge cases like high-frequency logging, log rotation, and failures.


This LLD problem tests OOP modeling, design pattern usage (Singleton, Strategy), concurrency, and extensibility for a real-world logging component.

Solution

Comments