Part 5 of 6 · 3 min read
Edge cases and changes
Lost logs, order and new outputs.
Step 5: edge cases, tests and change requests
Edge cases:
| Case | Behaviour |
|---|---|
| Appender throws | Caught, the other appenders still run |
| Queue full | The record is dropped and counted |
| Program exits with records queued | Lost unless close is called, so register it at shutdown |
| A child sets its own level | It overrides the parent for itself and its descendants |
| Same name asked twice | The same logger, because of the map |
| Two threads ask for a new name together | get is synchronized, so one logger is created |
| Message null | Decide and document: reject, or write "null" |
| Error with no message | The formatter prints the error text |
The main trade-off is in the async appender: dropping records versus blocking the caller. Dropping keeps the application fast and can lose the one line you needed. Blocking keeps every line and can stall a request when the disk is slow. A common middle path is to drop low levels (debug, trace) when the queue is nearly full and never drop errors. Say which you chose and why.
A second trade-off is order across threads. One worker keeps the order in which records were queued. Several workers would be faster and would mix the order.
Tests to write first: each row of the run table; level inheritance with three levels of nesting; the supplier called zero times when disabled; an appender that throws; the async appender with a full queue, checking that delivered plus dropped equals sent; and close flushing every queued record.
Change requests and the part that changes:
- "Write to a file." A
FileAppenderimplementingAppender. Nothing else changes. - "Roll the file every day or at 10 MB." A decorator or a policy object inside the file appender.
- "Output as JSON." A new
Formatter. - "Add the request id to every line." A per-thread context map, copied into the record when it is created, and read by the formatter.
- "Only show errors from one package at debug level." Already supported by setting that logger's level.
- "Filter by a rule, such as messages containing a word." A
Filterinterface that an appender or logger consults, as a chain. - "Send errors by email." An appender that only acts on error level, ideally wrapped in the async appender.
- "Reload the configuration at run time." Levels are volatile and appender lists are copy-on-write, so changing them while running is already safe.
Common mistake
Building the message string before checking the level, so that disabled debug calls still cost time. Pass a supplier, or check enabled first.
Interview angle
Answer in your head first, then open it.
Which patterns does a logger use?
Strategy for formatters, observer for appenders, a chain through the parent hierarchy, and decorator for async or filtered appenders.
How do you keep logging from slowing the application?
An async appender with a bounded queue and one worker, never blocking the caller. Say what happens when the queue is full.
How is the level decided?
The nearest ancestor with a level set, with a default at the root.
Why not a singleton?
A manager that is passed in is easy to replace in tests and shares no hidden state. A static accessor can sit on top of it.
Check yourself:
1. Which change request needs only a new Formatter?
2. What is the cost of dropping records when the queue is full?
3. Why does one worker keep records in order?
Words you will meet
| Word | Meaning |
|---|---|
| Log record | One entry: time, level, source, message and optional error |
| Threshold | The lowest level that a logger lets through |
| Effective level | The level in force after looking at the parent chain |
| Formatter | An object that turns a record into text |
| Flush | To write out everything still waiting in a queue |