Design a logging library
medium 75 minLevels, a tree of loggers, sinks and formatters, then the hard part: full disks, slow networks, lost lines and many threads.
Video lesson · Chapter 1 of 6
Make a prediction
See it run
One call, a tree of loggers, several sinks. Watch where a record stops and where it goes on.
Operations
A call that is refused
debug() while the level is INFO: the cheap way out.
Step 1 of 5
The tree
The pump writes to ship.engine. Only root has a level of its own: INFO.
Step 1: The tree
def enabled(self, level): node = self while (own := node._level) is None: node = node._parent return own <= level < Level.OFF- Cost
- Depth of the tree
- Guarantee
- Nothing is built
- Formatting first
- pays for every refused line
The refused call is the common call. Make it the cheap one.
In the real world
The same logging library doing real jobs. Pick one and step through it.
A user name with a line break inside.
Step 1 of 3
What the user typed
| Part | Text | |
|---|---|---|
| 1 | name | bob\nINFO admin logged in |
| 2 | message | login failed for %s |
| 3 | ||
| 4 |
The name is 'bob', a line break, and then text that looks like a log line.
Step 1: What the user typed
def handle(self, record): if record.level < self._level: return line = self._formatter.format(record) with self._lock: self._write(line)Common mistakes
The bugs interviewers watch for, with the fix beside each.
Formatting before the level check
majorThe text is built for lines that nobody wants.
Wrongmessage = template % argsif not self.enabled(level): returnRightif not self.enabled(level): returnmessage = template % argsOne lock for the whole logger
majorA slow network sink holds up the console and every thread.
Wrongwith manager.lock: for sink in sinks: sink.write(format(record))Rightline = self._formatter.format(record)with self._lock: # this sink only self._write(line)A queue without a limit
criticalA slow disk becomes a program without memory.
Wrongself._queue = deque()self._queue.append(record)Rightif len(self._queue) == self._size: self._dropped += 1 # decided and counted returnLetting a sink's error out
criticalA full disk stops a payment, and the next sink gets nothing.
Wrongfor sink in node._sinks: sink.handle(record)Rightfor sink in node._sinks: try: sink.handle(record) except Exception as e: self._on_error(sink, e)Asking every parent's level on the way up
minorAn INFO line from a DEBUG branch disappears at a WARN parent.
Wrongwhile node and node.enabled(level): node.write(record) node = node.parentRightif not self.enabled(level): # once, here return# then up the tree, sinks decide
Interview tips
Open with
- Is it wanted? Logger.
- Where does it go? Sink.
- How does it look? Formatter.
Say aloud
- The refused call is the common call, so it must cost almost nothing.
- A logger must never break the program it watches.
Before they ask
- Where can a line be lost? Level, full queue, crash.
- What is counted when that happens?
Avoid
- A static singleton that tests cannot replace.
- A level on every class by hand: use the tree.