Design a Logging Framework
📝 What is a logging framework? It is the library behind a line like
log.info("user created"). It decides whether a message is important enough to keep, turns it into a line of text, and writes that line to one or more places: the console, a file, an alerting channel. A good one does all this without slowing down the code that called it.
Interviewers ask this problem in several forms: "design a logger with levels", "design log appenders and formatters", "design an async logger with a bounded buffer", "design an audit log". They are one design. Levels and appenders decide where a message goes, formatters decide how it looks, the async buffer decides when it is written, and the audit trail is a second path with a much stricter promise. We cover all four.
Requirements
"Design a logging framework. Application code logs messages at different levels, and the framework writes them to several destinations in different formats. Logging must not slow the application down."
The last sentence is the one that makes this problem interesting. The questions below turn it, and the rest of the prompt, into rules.
Clarifying Questions
You: Which levels are there, and who filters on them?
Interviewer: Four: DEBUG, INFO, WARN, ERROR, from lowest to highest. The logger has a minimum level, and each destination has its own too. For example, the console shows everything from INFO up, and the alerts channel only ERROR.
You: How many destinations, and does each one choose its own format?
Interviewer: Any number, added at runtime, each with a name, a minimum level and a format. Give me three formats: a short one for people, a detailed one for files and JSON for machines.
You: "Must not slow the application down": can the caller wait while we write to a file?
Interviewer: No. Writing can be slow. Put records in a buffer and write them in the background. The buffer has a fixed size, though; we can't use unlimited memory.
You: What happens when the buffer is full?
Interviewer: That's configurable. Some services would rather wait than lose a line, others would rather lose a line than wait. Support blocking, dropping the new record and dropping the oldest one. And count what we drop.
You: Is there a way to force the buffer out, for example before shutdown?
Interviewer: Yes, a flush. After a flush returns, everything logged before it has been written.
You: You mentioned audit logs. How are they different from normal logs?
Interviewer: Audit entries record who did what, for security and compliance. They are never filtered by level, never dropped, and written right away. Each one gets a sequence number so a missing entry is easy to spot.
You: How do we get the time, and how do tests see the output?
Interviewer: Pass the timestamp in. For tests, destinations can keep their lines in memory, and the background writer is modelled by flush.
The fourth answer is a real design decision, not a detail. "Wait" and "drop" are opposite trade-offs between the application's speed and the log's completeness, and the right one depends on the service. That is why it is a setting.
💡 Interview tip: When the interviewer says "must not slow things down", ask what should happen when the system cannot keep up. Every bounded buffer fills up eventually. The answer to "then what?" is what turns a queue into a design.
Final Requirements
- Records have a timestamp, a level, a source (the part of the program that logged) and a message. Levels are ordered
DEBUG < INFO < WARN < ERROR. - The logger has a minimum level. A record below it is ignored right away and costs nothing more.
- Any number of named appenders can be added. Each has its own minimum level and a format:
SIMPLE,DETAILEDorJSON. logdoes not write. It puts the record into a bounded buffer of a fixed capacity.- When the buffer is full, the configured policy applies:
BLOCK(the caller waits until the writer makes room),DROP_NEWESTorDROP_OLDEST. Dropped records are counted. flushwrites every buffered record, oldest first, to every appender whose level lets it through.- Audit entries skip levels and the buffer, are written at once, are never dropped and carry gapless sequence numbers.
- Time is passed in with each call.
Out of scope
- Log rotation, compression and shipping logs over the network.
- Configuration files and per-module levels (Extensibility 1).
- Escaping special characters in messages.