Design a logging library with appenders and log level filtering
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a logging framework similar to Log4j that supports multiple appenders (Console, File, Database) and log levels (`DEBUG`, `INFO`, `ERROR`). The framework applies the Observer pattern: appenders are observers that receive log messages from the central logger.
Log levels have the following severity order, lowest to highest: `DEBUG < INFO < ERROR`. When a message is logged at a given level, it is forwarded to all registered appenders only if the message's level is greater than or equal to the framework's current minimum log level threshold. The default minimum log level is `DEBUG`.
Each appender is identified by a unique name and has a type (`CONSOLE`, `FILE`, or `DATABASE`). Appenders store messages in the format `"[LEVEL] message"`. You can retrieve the stored logs from any specific appender by its name.
Implement the following methods:
- `addAppender(name: str, appender_type: str) -> bool`: Register a new appender with the given name and type (`CONSOLE`, `FILE`, or `DATABASE`). Returns `True` if added successfully, `False` if the name already exists or the type is invalid.
- `removeAppender(name: str) -> bool`: Remove an appender by name. Returns `True` if removed successfully, `False` if the appender was not found.
- `setLogLevel(level: str) -> bool`: Set the minimum log level threshold (`DEBUG`, `INFO`, or `ERROR`). Messages below this level will be ignored. Returns `True` if the level is valid, `False` otherwise.
- `log(level: str, message: str) -> int`: Log a message at the specified level. The message is forwarded to all registered appenders if the level meets or exceeds the minimum threshold. Returns the number of appenders the message was sent to, or `-1` if the level is invalid.
- `getLogs(name: str) -> List[str]`: Retrieve all log entries stored in the specified appender. Returns an empty list if the appender is not found.
- `getAppenderType(name: str) -> str`: Return the type of the specified appender. Returns an empty string if the appender is not found.
- `getAppenderCount() -> int`: Return the number of currently registered appenders.
Example, one call per line with its return value:
```text
fw = LoggingFramework()
fw.addAppender("console", "CONSOLE") -> True
fw.addAppender("file", "FILE") -> True
fw.addAppender("file", "DATABASE") -> False # name already taken
fw.addAppender("db", "QUEUE") -> False # invalid type
fw.log("DEBUG", "cache warmed") -> 2
fw.setLogLevel("INFO") -> True
fw.log("DEBUG", "cache miss") -> 0 # below INFO, dropped
fw.log("ERROR", "disk full") -> 2
fw.log("WARN", "slow query") -> -1 # invalid level
fw.removeAppender("console") -> True
fw.log("INFO", "retrying") -> 1
fw.getLogs("file") -> ["[DEBUG] cache warmed", "[ERROR] disk full", "[INFO] retrying"]
fw.getLogs("console") -> [] # appender removed
fw.getAppenderType("console") -> ""
fw.getAppenderType("file") -> "FILE"
fw.getAppenderCount() -> 1
```
```hint Comparing levels
Comparing the level names as strings does not give `DEBUG < INFO < ERROR`. Decide what each level should map to so the threshold check is a single comparison.
```
```hint Two ways to send nothing
`log` can reach zero appenders for more than one reason, and only one of them is an error. Check the order in which you validate.
```
### Constraints & Assumptions
- Log levels are `DEBUG`, `INFO` and `ERROR`, with severity `DEBUG < INFO < ERROR`.
- Valid appender types are `CONSOLE`, `FILE` and `DATABASE`.
- Appender names are unique strings.
- The default minimum log level is `DEBUG`.
- Stored entries use the format `"[LEVEL] message"`.
- At most 1000 method calls are made in total.
### Clarifying Questions to Ask
- Are level and type strings matched exactly as uppercase, or should `"info"` be accepted too?
- If an appender is removed and later added again under the same name, should its old entries come back?
- Is there only the one framework-wide threshold, or can each appender have its own minimum level?
- Do the appenders need to write to a real console, file or database here, or only keep their entries in memory?
- Will `log` be called from several threads at once?
### What a Strong Answer Covers
- A clear Observer structure: the logger as subject, a common appender interface, and a registry keyed by unique name, so a new appender type needs no change to `log`.
- Level handling through ordered severities, with an invalid level (`-1`) kept separate from a filtered or undelivered message (`0`).
- The exact return value of every method on bad input: duplicate names, unknown types, unknown appenders, invalid levels.
- State details: the stored entry format, what happens to an appender's entries when it is removed, and why `getLogs` should not expose the internal list.
- The cost of each operation, and how the design would change for per-appender levels, concurrency or slow sinks.
### Follow-up Questions
- How would you give each appender its own minimum level, and what should `log` return then?
- How would you make the framework safe to call from many threads at once?
- Writes through the database appender are slow. How do you keep `log` from blocking the caller, and what happens when the backlog grows?
- How would you let each appender format entries differently (timestamps, JSON) without changing the logger?
Overview: Design a Log4j-style logging library with console, file and database appenders and three log levels, DEBUG, INFO and ERROR. It tests the Observer pattern, threshold filtering by severity, exact return values for invalid input, and how the design extends to per-appender levels, thread safety and slow sinks.