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.
It must
- Four levels, in order: DEBUG, INFO, WARN, ERROR. Each destination has a minimum level.
- Messages can go to several destinations at once (console, file, database).
- Each message has a level, text and timestamp.
- The format of a message is configurable (plain text, JSON).
- The whole application uses one shared logger and it is safe to call from many threads.
Keep in mind
- Adding a new destination (email, remote service) must not change existing code.
- Logging must be cheap when the level is too low: skip the work early.
- Log rotation, async batching and structured fields are follow-ups.