Logging Framework (Log4j): low-level design

Design a logging library like Log4j. Application code logs messages with a level (debug, info, warning, error). The library sends each message to one or more destinations, such as the console, a file or a database, in a chosen format.

Stage 1: Levels and messages

Say what a log line is and who creates it.

Before any destination or format, we need the basic idea of a log line and a way for code to create one. A `LogLevel` enum with an order, a `LogMessage` (level, text, timestamp) and a `Logger` with one method per level. Each method builds a message and passes it on, but only if its level is at least `minLevel`. Levels are ordered, so 'at least INFO' is a simple comparison. Checking the level first means debug calls cost almost nothing in production. Putting the level on the message (not just the method) lets later stages route by level. It costs one field.

Stage 2: One logger for the whole app

Share a single logger safely.

If every class creates its own `Logger`, each has its own settings and writes to its own place, so output is scattered and levels disagree. `Logger` gets a private constructor and a `getInstance()` method. Every part of the program receives the same instance. This is the **Singleton** pattern: one object, one configuration, one place to change the level. Two threads can race on the first `getInstance()` and create two loggers. Create it eagerly at startup, or guard creation with a lock. A singleton also makes testing harder, so expose it through an interface when you can.

Stage 3: Many destinations: a chain of handlers

Send each message to the right places without the logger knowing them.

The logger has nowhere to send messages, and 'print to console' hard-coded inside it would make a file destination an edit to the logger. A `LogHandler` base class with a link to the `next` handler. `handle()` writes the message if its level is high enough for this handler, then always passes it on. `ConsoleHandler`, `FileHandler` and `DatabaseHandler` implement `write()`. This is **Chain of Responsibility**: the logger only knows the first handler. A console handler at INFO and a file handler at ERROR means errors go to both and info only to the console. A new destination is one new class. Every message walks the whole chain even if only the first handler wants it. The chain is short, so that is cheap. Alternatively treat each handler as an observer of the logger.

Stage 4: Choose the format

Change how a message looks without touching destinations.

Every destination would format messages its own way, and a JSON log file would need a different file handler. A `Formatter` interface turns a `LogMessage` into text. `PlainFormatter` and `JsonFormatter` implement it. Each handler holds a formatter and writes the finished text. Formatting and destination vary independently, so each is its own piece (**Strategy**). Console gets plain text, the file gets JSON, and neither handler knows how the other formats. Formatting happens per handler, so a message can be formatted twice. If that becomes costly, format once and share the string.