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.

Watch lesson4:58

Where it shows up

The same pattern appears across everyday software.

  • Python logging

    Loggers form a tree by dotted names. With `propagate` set, events go on to the handlers of the ancestors. `QueueHandler` and `QueueListener` move slow handlers to another thread.

  • Logback for Java

    Its manual gives the basic selection rule: a request of level p is enabled if p is at or above the effective level q of the logger, which comes from the nearest ancestor that has one.

  • Apache Log4j 2

    Asynchronous loggers use a ring buffer. What happens when it is full is set by a policy object, `AsyncQueueFullPolicy`.

  • Uber's zap for Go

    Its production configuration samples: repeated logs within a second are thinned on purpose. The FAQ names this as the reason for lines that seem to be missing.

  • Go's log/slog

    A front part makes records, a `Handler` decides and writes. Text and JSON handlers come with it. A `LogValuer` lets a type decide what is logged, for example to hide a secret.

  • spdlog for C++

    Sinks for console, rotating and daily files that can be combined in one logger, and an optional asynchronous mode with a thread pool.

  • systemd journald

    Limits each service: by default 10,000 messages in 30 seconds. Further messages in the interval are dropped, and a message tells how many.

  • Docker log drivers

    Delivery is direct and blocking by default. The non-blocking mode puts an in-between buffer there, so that a slow driver does not stop the application.

  • logrotate on Linux

    Renames files from outside and can tell the program to open its log again. `copytruncate` is for programs that cannot, and its manual warns that some lines may be lost.

  • MDC in Logback

    Keeps request data for each thread. The manual says that a child thread does not inherit it and that every put should be balanced by a remove.