Explanation of Each Class
Logger
- Central class that the developer uses.
- Manages the current log level, formatter, and output handler.
- Filters messages and sends them to appropriate destination.
LogHandler (Abstract or Interface)
- Defines the method
write()
for outputting logs. - Concrete implementations:
-
-
-
FileHandler: Writes logs to a file.
-
ConsoleHandler: Prints logs to the terminal.
-
-
Formatter
- Formats log messages with timestamps, levels, etc.
- Can be extended for custom formatting (e.g., JSON format).
LogLevel
- Enum or constants defining severity levels.
- Used by
Logger
to determine which messages to process.
How to Teach This
- Explain how separation of concerns is maintained through handlers and formatters.
- Use examples to show how a developer can plug in different formatters or handlers.
- Emphasize extensibility, where more handlers like
RemoteHandler
orDatabaseHandler
can be added later.