00:00

Problem Statement

Input/Output Examples

Visual Explanation

Each output is a strategy: the logger does not know whether a line goes to the console, a file or a network service.

Approaches

Both approaches filter by level the same way. The first sends a line to a list of outputs; the second passes it along a chain of handlers.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

A filtered message costs one comparison. A written message is formatted once and passed to each of the o outputs.

Interview Tips and Recommended Approach

  • Filter before formatting. Building the string for a message that is dropped wastes time.
  • Use a Supplier<String> for expensive messages so they are built only when needed.
  • Mention thread safety of the outputs; a shared file writer needs synchronisation or a queue.
  • Know SLF4J and Logback. In real projects, use a logging library rather than writing one.

Recommended Approach: Enum Levels and Output List

Why: the enum gives an ordered set of levels for free, and a list of outputs is the simplest way to support several destinations.