Design a logging framework
Design a logging library that an application uses to write messages at different levels to different destinations.
Good first question for: SDE-1
Requirements to confirm first
- Levels: debug, info, warn, error; messages below the configured level are dropped
- Several destinations at once: console, file, a remote collector
- Each destination can have its own level and its own format
- Logging must not slow down or crash the code that calls it
- Ask: synchronous or asynchronous? File rotation? Structured key-value logs?
Write your own class list on paper before you open the answer key. Ten minutes of your own design is the practice; reading the key is only the check.
Show the answer key (write yours first)
Classes and what each one owns
- Logger
- What application code calls; checks the level and hands the event on
- LogEvent
- Time, level, message, thread and optional fields
- Appender
- Interface for one destination: console, file, network
- Formatter
- Interface that turns an event into a line of text or JSON
- LoggerConfig
- The level and the appenders, possibly per logger name
- Level
- An ordered enum, so 'at least WARN' is one comparison
How they connect
- Logger has many Appenders, and each Appender has one Formatter
- Logger does not know where output goes; a new destination is a new class and a line of configuration
- An asynchronous appender wraps another appender and owns a bounded queue
Design patterns that fit, and why
- Strategy
- Formatter: the same event as plain text for the console and as JSON for a collector.
- Decorator
- Asynchronous writing, buffering or filtering wrapped around any appender.
- Observer
- Logger publishes one event to every registered appender.
- Chain of Responsibility
- Sometimes proposed for levels. An ordered comparison does the same job with less code; say so.
Code sketch in Java
The core types and one real method. It is a sketch to talk through, not a full program.
enum Level { DEBUG, INFO, WARN, ERROR }
interface Appender { void append(LogEvent event); }
interface Formatter { String format(LogEvent event); }
class Logger {
private volatile Level level;
private final List<Appender> appenders = new CopyOnWriteArrayList<>();
void log(Level at, String message) {
if (at.compareTo(level) < 0) return; // cheap exit first
LogEvent event = new LogEvent(Instant.now(), at, message);
for (Appender a : appenders) {
try { a.append(event); } catch (RuntimeException e) { /* never break the caller */ }
}
}
}Edge cases to raise yourself
- Many threads write to one file: lines must not interleave
- The asynchronous queue is full: block, drop everything, or drop only debug? Choose and say why
- A destination is down: the disk is full or the network is gone
- An expensive message string is built and then dropped by the level check
Where answers usually go wrong
- One Logger class with a switch on the destination type. Every new destination edits the core class.
- File or network writes on the caller's thread without saying so. Either accept the cost out loud or add an asynchronous appender with a bounded queue.
- An unbounded queue for asynchronous logging. Under load it eats the memory of the service it is meant to observe.
- Letting an exception from an appender reach the application.
Follow-up questions
- Add file rotation by size and by day
- Change the level of one package while the program runs
- Guarantee that error logs are not lost on a crash