Filter Logs by a Typed Level

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Log levels go debug, info, warn, error, from least to most serious. Export a LogLevel type that is exactly those four strings, shouldLog(level, minimum), true when level is at least as serious as minimum, and isLogLevel(text), true only for the four names, so a level read from config can be checked first. Type them so passing any other string as a level is a type error, and so isLogLevel narrows a string to LogLevel.

Your code must pass

  • Logs at or above the minimum
  • Skips levels below the minimum
  • Recognises only the four levels
  • Has no type errors
  • LogLevel is exactly the four levels
  • Rejects an unknown level
  • isLogLevel narrows a string