Part 2 of 6 · 2 min read
Requirements
What a log call does, and where it goes.
Think of a ship's logbook. The crew writes entries of different seriousness: a routine position, a warning about weather, an emergency. The captain may ask for routine entries to be skipped on a calm day. The entries may also be copied to a radio log and to the company office. A logging library is that logbook.
Questions and the answers assumed here:
- Which levels? Trace, debug, info, warn and error, in that order of seriousness.
- How are entries filtered? A logger has a threshold. Entries below it are dropped.
- Where do entries go? To appenders, such as the console, a file or memory. More than one at once.
- How are they written out? A formatter turns a record into text, and can be replaced.
- Can parts of the program have their own settings? Yes. Loggers are named with dots, such as "app.db.pool", and a child inherits from its parent.
- Must logging slow the program? No. A slow disk must not slow a request.
- Out of scope: rolling files, remote shipping, and configuration files.
The numbered requirements:
- R1. A log call has a level, a message and optionally an error.
- R2. A call below the logger's effective level is dropped, and its message is not even built if given as a supplier.
- R3. A logger with no level of its own uses its nearest parent's level.
- R4. A record goes to the logger's own appenders and to those of every parent.
- R5. A failing appender never breaks the caller or the other appenders.
- R6. An async appender takes records off the caller's thread and never blocks it.
- R7. Loggers are found by name, and the same name gives the same logger.
Appender
An appender is an object that receives log records and writes them somewhere, such as a console, a file or a list in memory.
Example: MemoryAppender, which keeps formatted lines in a list for tests.
Check yourself: 1. What does a logger do with a call below its level? 2. How does "app.db.pool" relate to "app.db"? 3. Why must logging not slow the caller?