diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-08-29 19:12:39 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-08-29 19:12:39 -0500 |
| commit | e2bc14a1572c4a3d035ac7a4ffe47cff35fd2e16 (patch) | |
| tree | 64acddab08f5a4ed3982017e20993ae99b7845f8 /src/slskd/Core/API | |
| parent | ea4ca200cd6dda759917dcd76d47d2bddfa5d2f5 (diff) | |
| download | slskd-e2bc14a1572c4a3d035ac7a4ffe47cff35fd2e16.tar.gz | |
parse logs with the regex
Diffstat (limited to 'src/slskd/Core/API')
| -rw-r--r-- | src/slskd/Core/API/Controllers/LogsController.cs | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/src/slskd/Core/API/Controllers/LogsController.cs b/src/slskd/Core/API/Controllers/LogsController.cs index d7e011eb..1c1f057b 100644 --- a/src/slskd/Core/API/Controllers/LogsController.cs +++ b/src/slskd/Core/API/Controllers/LogsController.cs @@ -204,14 +204,52 @@ namespace slskd.Core.API continue; } - var parts = line.TrimStart('[').Split(']', count: 2); - var meta = parts[0].Split(' '); + /* + there should be 3 possible line types: + + debug: + [Some.Context] [2000-1-1T11:11:11 WRN] foo bar + + info: + [2000-1-1T11:11:11 WRN] foo bar + + line wrap/newline: + foo bar + + additionally, date was added to disk log files around 9/1/26, so we need to gracefully handle + cases where the timestamp contains only hh:mm:ss, and substitute the unix epoch for the date + + the regex includes 8 matching groups (hopefully this comment and the regex don't diverge!) + using the debug log as an example, the groups are: + + 0. [Some.Context] [2026-08-29T11:11:11 WRN] foo bar + 1. [Some.Context]<space> + 2. Some.Context + 3. [2000-01-01T11:11:11 WRN]<space> + 4. 2000-01-01T + 5. 2000-01-01 + 6. 11:11:11 + 7. WRN + 8. foo bar + + the final group in the regex is simply `.*`, so we're guaranteed to get a match for every line + */ + match = LogLineParseRegex.Match(line); + + var grp = match.Groups; + + var date = grp[5].Success ? DateOnly.Parse(grp[5].Value) : DateOnly.FromDateTime(DateTime.UnixEpoch); + var time = grp[6].Success ? TimeOnly.Parse(grp[6].Value) : TimeOnly.FromDateTime(DateTime.UnixEpoch); + var dateTime = date.ToDateTime(time); + + var level = levels.ContainsKey(grp[7].Value) ? levels[grp[7].Value] : null; list.Add(new LogRecord { - Timestamp = DateTime.Parse(meta[0]), - Level = levels[meta[1]], - Message = parts[1].TrimStart(), + Context = grp[2].Value, + Timestamp = dateTime, + Level = level, + Message = grp[8].Value, }); } catch (Exception ex) |
