WHILELOOPWARRIOR(1)Design Interview ManualWHILELOOPWARRIOR(1)

~/lld/logger

Logger · 9001

A easy low-level design interview question on strategy, chain of responsibility, open/closed. 7 scenario tests.

SYNOPSIS

interviewer: Design a logging library.

Application code calls logger.info("..."). Your library decides what to keep, formats it and writes it somewhere. About 30 minutes.

QUESTIONS TO ASK

Interviews start vague on purpose. Strong candidates ask before they draw; in the studio the interviewer answers these and the answers become your requirements.

  • Which log levels do we need?
  • Where do log lines go?
  • What output formats?
  • Do we need named or child loggers?
  • How do we get timestamps in tests?
  • What if someone sets an unknown level?
  • Must logging be asynchronous?
  • Do we rotate log files?

DESCRIPTION

The full statement, as the studio shows it at the implement step.

Design a small logging library. Application code calls logger.info("...") and the library decides whether the message is important enough, formats it and hands it to every configured destination (an "appender").

requirements

  • Four levels, lowest to highest: DEBUG, INFO, WARN, ERROR.
  • A logger has a minimum level (default INFO). Messages below it are dropped.
  • A logger can have several appenders. Each appender has its own formatter and its own minimum level (default DEBUG, i.e. everything that reaches it).
  • Two formatters: PlainFormatter and JsonFormatter.
  • logger.child("db") returns a logger named "db" (or "app.db" when the parent is named "app") that writes to the same appenders and follows the parent's level unless setLevel() is called on the child itself.
  • Time comes from an injectable clock (options.clock, default Date.now), so tests are deterministic.

output formats

  • Plain: "<LEVEL> <message>", or "<LEVEL> [<name>] <message>" when named.
INFO server started
WARN [app.db] slow query
  • Json: JSON.stringify of { time, level, name, message } in that key order; "name" is left out when the logger has no name.
{"time":1000,"level":"ERROR","message":"disk full"}

rules

  • setLevel() with an unknown level throws an Error whose message contains "unknown level".
  • MemoryAppender keeps formatted lines in order; .lines returns a copy.

think about

  • Adding a FileAppender or a new format should not touch Logger.
  • Where does the level check live, and why there?

GRADING

The studio runs the interview in seven steps (clarify, entities, class diagram, patterns, sequence, implement, review) and a coach grades each one live: the core objects you found, the relations between them (ownership vs. use, the right end of the diamond), responsibilities, whether your patterns really have the shape of the pattern, and a sequence diagram of the main use case. Then you implement it in JavaScript and run its 7 scenario tests in your browser. The review gives a score out of 100, a hire signal and a reference design to compare.

FOLLOW-UPS

What the interviewer asks when you finish early:

  • Make logging asynchronous: appenders write in batches without losing order. What happens to messages when the process crashes?
  • Add per-appender filters (e.g. only messages from "app.db").

SEE ALSO