Logging Framework
Customizable Logger
Design the logging library an application calls: named loggers with levels, handlers that write to different places, pluggable formats, and a queue so slow destinations never stall the caller.
Overview
An application calls log.info("order placed", order_id=42) and moves on. Behind that one line a library decides whether the message matters, turns it into text or JSON, and writes it to a console, a file, or a remote service, from many threads at once, without slowing the application down. You are designing that library.
The problem is popular because it is small enough to code in an hour yet touches most of the design vocabulary interviewers want to hear: a pluggable format, a pluggable destination, a tree of named loggers with inherited levels, a lock around I/O, and a queue with a worker thread. It also has real reference designs: Python's logging module and Java's Log4j 2 share the same broad shape (loggers, levels, handlers or appenders, formatters or layouts), and the code below follows it so you can point at it.
Scoping the problem
The library takes its shape from seven questions. Ask each one, and note the usual answer.
- Which levels, and can a handler have a level of its own? Use DEBUG, INFO, WARNING, ERROR and CRITICAL, and yes, each handler has its own. Filtering then happens twice: at the logger and at each handler.
- One global logger or named loggers? Named, in a dotted tree that inherits levels. Named loggers let you turn up one module without flooding the rest.
- Which destinations? Console and file now, a network sink later. Each is a class, and the framework must not care which.
- Text or structured output? Both, chosen per handler. A formatter is a strategy, so both must be possible.
- Must logging never block the caller? Yes, for slow sinks. That adds a queue and a worker thread, and a policy for when the queue is full.
- Many threads? Yes, so each handler takes a lock around the write.
- What if a destination fails? Count the error and keep going. A broken disk must not crash the app.
Functional requirements
- Five levels, DEBUG to CRITICAL, each an integer; a record is written when its level is at or above the threshold
- Named loggers in a dotted tree; a logger without its own level inherits from the nearest ancestor that has one
- Any number of handlers per logger; a record reaches the handlers of the logger and of its ancestors unless propagation is turned off
- Each handler has its own level and its own formatter
- Two formats: a text line and a JSON document; both carry structured fields passed as keyword arguments
- Destinations: a stream (console), a file, memory (for tests), and an asynchronous wrapper around any of them
- A simple call site: log.info(message, key=value)
Non-functional requirements
- Safe for many threads: a lock per handler around the write, none around the level check
- A record below the effective level costs a level lookup and one comparison; no record is built and no handler runs
- A slow destination behind AsyncHandler never blocks the caller unless the queue is full and blocking is chosen
- A handler that raises counts the error and does not crash the application or stop the other handlers
- Adding a destination or a format is one new subclass; nothing existing changes
Interview questions
- Why does a handler have its own level when the logger already has one?
- Walk me through the asynchronous handler. What can go wrong?
- Why not a single global logger, a Singleton?
- How would you support structured logging?
- Where exactly do you need locks, and why not one global lock?
- How do you test a logging library without reading files or the console?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio