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.1Split the line into tokens on runs of space characters (leading and trailing spaces are ignored). Then:
- Token 1 is the timestamp. It must have exactly the form
YYYY-MM-DDTHH:MM:SSZand 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.timestampin the result is the token exactly as given. - Token 2 is the level: exactly one of
DEBUG,INFO,WARN,ERROR(case-sensitive). - If the next token has the form
[name]with a non-emptyname,moduleisname. Otherwise the line has no module and that token belongs to the rest. - Fields: working backwards from the last token, every token of the form
key=valueis a field, wherekeymatches[a-z_][a-z0-9_]*andvalueis 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. messageis 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;levelis that token, or""if the line has only one token.