Design a logging library 00:00:00 Make a prediction You will route one record through logger and handler decisions and explain how an apparently harmless configuration can duplicate output. A logger has a console handler and propagates to its parent, which writes the same kind of console output. Can one accepted event print twice? Yes. One record can traverse more than one configured handler. Trace the routing tree, propagation settings and handler thresholds before assuming two application calls occurred. 00:00:32 The talking tubes of the good ship Pelican On a ship, speaking tubes carry messages to different stations. A note audible on the engine tube and then forwarded to the bridge can be written twice if both clerks copy it to the same ledger. Distinguish creating a log record, deciding which handlers receive it, and formatting each output. The record's route is part of the observable behaviour. A logging library takes short reports from all parts of a program and brings the wanted ones to the right places in the right form. The program says what happened. The library decides whether, where and how it is written. print cannot be switched off, has no place but one, and knows no importance. As soon as two people want different things from the same program, the decision must leave the line that reports. 00:01:20 Route a single log event An application component emits an informational event. The logger first decides whether the configured effective level admits it. Construct a record with its time, name, level and message. Pass the record to configured handlers; each can apply its own level and formatting. If a handler is attached both to the logger and through propagation to its parent, the same event may be written twice. Test the routing rather than counting logger objects. Send one warning to the file and console sinks. Simulate a console error and specify whether the file still receives the record. A logger used across an application needs predictable isolation here: a formatting failure or a secondary sink outage should not silently change a successful business action. 00:02:11 Overload an asynchronous sink A file sink slows down and the logging queue reaches capacity. Decide which levels may be dropped, whether a caller can block, and how to observe dropped messages. An unbounded queue makes slow output a memory problem. Stop the disk writer and fill two slots. When a third record arrives, a bounded logger needs an explicit result. Security-critical audit records may warrant a different policy from debug traces. Count any dropped records through a path that cannot recursively log back into the same full queue. An unbounded asynchronous logging queue can turn a slow disk into an out-of-memory problem. Decide how to surface dropped or delayed messages. For small applications, a direct synchronous handler is easier to reason about until actual latency demands buffering. 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. 00:03:31 Try an interview follow-up Why are sink and formatter two interfaces? They vary apart from each other. With one class for each mixture, 3 places and 2 looks need 6 classes, and one more place needs 2 again. Apart they are 3 plus 2, and the next of either kind is one class. One application log call produces two identical console lines. Where would you trace before changing the message producer? Follow the record from the child logger to its own sink and then through propagation to the parent. The same sink may receive it twice. Change handler placement or propagation according to the intended routing contract, then test one record end to end. 00:04:17 What to take with you One call goes through three questions. The logger asks if it is wanted, without building anything. The sinks on the way to root ask if it is wanted there. The formatter makes one line of it. Place and look can be combined freely because they are two objects. The hard half is failure. Sinks break, so each call has its own try. Disks are slow, so slow sinks get a bounded queue that drops small levels first and counts them. Programs end, so close refuses, wakes, waits with a limit and closes. For each of these, say which lines are lost.