AlgoViz

Design a logging library

medium 75 min

Levels, 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

0:00 / 4:58

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

parentparentpumpcallership.engineLoggerlevelNoneshipLoggerlevelNonerootLoggerlevelINFO

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

PartText
1namebob\nINFO admin logged in
2messagelogin 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

    major

    The text is built for lines that nobody wants.

    Wrong
    message = template % argsif not self.enabled(level):    return
    Right
    if not self.enabled(level):    returnmessage = template % args
  • One lock for the whole logger

    major

    A slow network sink holds up the console and every thread.

    Wrong
    with manager.lock:    for sink in sinks:        sink.write(format(record))
    Right
    line = self._formatter.format(record)with self._lock:          # this sink only    self._write(line)
  • A queue without a limit

    critical

    A slow disk becomes a program without memory.

    Wrong
    self._queue = deque()self._queue.append(record)
    Right
    if len(self._queue) == self._size:    self._dropped += 1    # decided and counted    return
  • Letting a sink's error out

    critical

    A full disk stops a payment, and the next sink gets nothing.

    Wrong
    for sink in node._sinks:    sink.handle(record)
    Right
    for 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

    minor

    An INFO line from a DEBUG branch disappears at a WARN parent.

    Wrong
    while node and node.enabled(level):    node.write(record)    node = node.parent
    Right
    if 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.