Implement Logger service

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).

Logging Destinations

•Console logging.
•File logging with rotation (daily, size-based).
•Remote logging (e.g., HTTP endpoint, centralized logging system).

Formatting

•Support customizable log formats including timestamp, log level, message, thread info, and context.

Asynchronous Logging (Optional)

•Non-blocking logging using queues or background workers.

Configuration

•Enable/disable log levels dynamically.
•Configure output destinations at runtime.

Error Handling

•Handle failures in writing logs gracefully without affecting the main application.


Non-Functional Requirements (NFR)


•Performance: Minimal impact on application performance.
•Thread-Safety: Safe for concurrent logging from multiple threads.
•Scalability: Handle high log throughput.
•Extensibility: Easy to add new destinations, formats, or filters.
•Reliability: Ensure logs are persisted even in case of application crash.


Core Entities & Attributes


Class / Module Responsibilities

Logger Core class to log messages (log(level, message, context))

LogLevel Enum: DEBUG, INFO, WARN, ERROR, FATAL

Appender / Handler Base class for output destinations (ConsoleAppender, FileAppender, RemoteAppender)

Formatter Defines log message format (timestamp, level, message, thread)

LogQueue / AsyncWorker Optional background thread to process log entries asynchronously

LoggerConfig Configurations for log levels, appenders, and formats


Key Operations / APIs


•log(level, message, context) → logs a message with given level and optional context
•debug(message) → shorthand for log(DEBUG, message)
•info(message) → shorthand for log(INFO, message)
•warn(message) → shorthand for log(WARN, message)
•error(message) → shorthand for log(ERROR, message)
•addAppender(appender) → attach a new output destination
•setFormatter(formatter) → customize log format
•enableLevel(level) / disableLevel(level) → dynamically configure log levels


Design Considerations


Thread Safety


•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

1 2logger = Logger() 3logger.addAppender(ConsoleAppender()) 4logger.addAppender(FileAppender("app.log", rotate_daily=True)) 5logger.setFormatter(SimpleFormatter()) 6 7logger.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
•Error handling and resilience in system design


Solution

Comments