A logging framework is a software library or tool that provides a standardized way to record, format, filter, and route log messages generated by an application during its execution.
Logging is critical for:
Well-designed frameworks abstract the complexity of logging so developers can focus on writing code without worrying about how logs are processed or where they go.
Frameworks like Log4j, SLF4J, and java.util.logging power millions of production services.
But what does it take to build such a system from scratch?
In this chapter, we will explore the low-level design of a logging framework in detail.
Lets start by clarifying the requirements:
Before starting the design, it's important to ask thoughtful questions to uncover hidden assumptions and clearly define the scope of the system.
Here is an example of how a discussion between the candidate and the interviewer might unfold:
Candidate: What log levels should the logging framework support?
Interviewer: It should support standard log levels like DEBUG, INFO, WARN, ERROR and FATAL. The system should filter messages based on a configured threshold (minimum log level).
Candidate: Do we need to support writing logs to multiple destinations?
Interviewer: Yes, the framework should support multiple destinations, including the console and file outputs.
Candidate: Can a single log message be sent to more than one destination simultaneously?
Interviewer: Yes, a log message can be routed to multiple output destinations at the same time.
Candidate: Do we need to support multiple log message formats ?
Interviewer: : Yes. Every log entry should include a timestamp and log level at a minimum. The system should support plugging in custom formatters.
Candidate: Will the framework be used in a multi-threaded environment? Do we need to ensure thread safety?
Interviewer: Yes, it will be used in a multi-threaded environment. The logging system must be thread-safe to ensure that log messages are not interleaved or lost.
Candidate: Should the framework support asynchronous logging to prevent blocking the main application thread?
Interviewer: That’s an important consideration. Yes, the framework should support asynchronous logging.
After gathering the details, we can summarize the key system requirements.