Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

Markdown · 1.7 KiB · at 74947de

Write parseLogLine, which parses one log line such as:

Code
2024-03-05T10:20:30Z WARN [auth] login failed user=bob ip=10.0.0.1

Split the line into tokens on runs of space characters (leading and trailing spaces are ignored). Then:

  1. Token 1 is the timestamp. It must have exactly the form YYYY-MM-DDTHH:MM:SSZ and be a real UTC calendar time: month 01-12, a day that exists in that month (Gregorian leap years), hour 00-23, minute 00-59, second 00-59. timestamp in the result is the token exactly as given.
  2. Token 2 is the level: exactly one of DEBUG, INFO, WARN, ERROR (case-sensitive).
  3. If the next token has the form [name] with a non-empty name, module is name. Otherwise the line has no module and that token belongs to the rest.
  4. Fields: working backwards from the last token, every token of the form key=value is a field, where key matches [a-z_][a-z0-9_]* and value is non-empty (split at the first =; the value may contain further =). Stop at the first token from the end that is not a field; it and everything before it are not fields. If a key appears more than once, the rightmost occurrence wins.
  5. message is the remaining tokens between the level/module and the fields, joined with single spaces. It may be empty.

Result: { timestamp, level, module (absent when there is none), message, fields }, where fields is a plain object mapping each key to its value string.

Failures, checked in this order:

  • { kind: "empty_line" } when the line is empty or only spaces.
  • { kind: "bad_timestamp" } when the first token is not a valid timestamp.
  • { kind: "bad_level", level } when the second token is not a valid level; level is that token, or "" if the line has only one token.