feat: add option to filter logs by unit or slice

This commit is contained in:
nhnn 2024-07-25 20:34:28 +03:00 committed by Inex Code
parent ae2686e799
commit e9e4cf680b
2 changed files with 14 additions and 1 deletions

View file

@ -75,6 +75,10 @@ class Logs:
up_cursor: str | None = None, up_cursor: str | None = None,
# All entries returned will be greater than this cursor. Sets lower bound on results. # All entries returned will be greater than this cursor. Sets lower bound on results.
down_cursor: str | None = None, down_cursor: str | None = None,
# All entries will be from a specific systemd slice
filterBySlice: str | None = None,
# All entries will be from a specific systemd unit
filterByUnit: str | None = None,
) -> PaginatedEntries: ) -> PaginatedEntries:
if limit > 50: if limit > 50:
raise Exception("You can't fetch more than 50 entries via single request.") raise Exception("You can't fetch more than 50 entries via single request.")
@ -82,7 +86,7 @@ class Logs:
list( list(
map( map(
lambda x: LogEntry(x), lambda x: LogEntry(x),
get_paginated_logs(limit, up_cursor, down_cursor), get_paginated_logs(limit, up_cursor, down_cursor, filterBySlice, filterByUnit),
) )
) )
) )

View file

@ -24,9 +24,18 @@ def get_paginated_logs(
up_cursor: str | None = None, up_cursor: str | None = None,
# All entries returned will be greater than this cursor. Sets lower bound on results. # All entries returned will be greater than this cursor. Sets lower bound on results.
down_cursor: str | None = None, down_cursor: str | None = None,
# All entries will be from a specific systemd slice
filterBySlice: str | None = None,
# All entries will be from a specific systemd unit
filterByUnit: str | None = None,
): ):
j = journal.Reader() j = journal.Reader()
if filterBySlice:
j.add_match("_SYSTEMD_SLICE=" + filterBySlice)
if filterByUnit:
j.add_match("_SYSTEMD_UNIT=" + filterByUnit)
if up_cursor is None and down_cursor is None: if up_cursor is None and down_cursor is None:
j.seek_tail() j.seek_tail()