Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2022-07-20
Problem Statement
Design a Logger Service that allows applications to log messages with different severity levels, persist them to various outputs (console, files, remote storage), and optionally support asynchronous logging, formatting, and filtering.
Functional Requirements (FR)
Log Levels
•Support standard log levels: DEBUG, INFO, WARN, ERROR, FATAL.
•Allow filtering based on log level (e.g., only log WARN and above).
•Use synchronized structures or thread-safe queues for asynchronous logging.
Asynchronous Logging
•Avoid blocking application threads by using a queue + worker thread for writing logs.
File Management
•Support log rotation based on size or date.
•Handle file I/O exceptions gracefully.
Extensibility
•Easily add new appenders (e.g., KafkaAppender, CloudWatchAppender).
•Add new formatters (JSON, plain text, CSV).
Performance Optimization
•Buffer log messages before writing to disk or network.
•Batch writes for remote destinations.
Example Usage
12logger =Logger()3logger.addAppender(ConsoleAppender())4logger.addAppender(FileAppender("app.log", rotate_daily=True))5logger.setFormatter(SimpleFormatter())67logger.info("Application started")8logger.error("Failed to connect to DB", c{"db_host":"localhost"})
✅ This LLD problem tests:
•OOP design with modularity (Logger, Appender, Formatter)
•Concurrency and thread-safety
•Extensibility for future log destinations and formats