Skip to content

Pagination and export

Page through search results with cursors and export every matching event as NDJSON, JSON or CSV.

Updated

A search returns one page of events: 100 by default, and at most 1,000 or the server’s max_page_size if that is lower. To read more, follow the cursor. To work with the whole window, loop over the pages and write them to a file.

Follow the cursor

When more events match than fit in the page, the response includes next_cursor. Send it back as cursor to get the next page. Stop when next_cursor is null.

--cursor requires explicit --from and --to, because a relative --since window would move between runs.

logbrook query checkout --from 1791334800000 --to 1791338400000 --limit 100 --json
logbrook query checkout --from 1791334800000 --to 1791338400000 --limit 100 \
  --cursor '<next_cursor>' --json

In table output the cursor is on the final NEXT_CURSOR row. Each run fetches one page.

curl --fail-with-body --silent --show-error --get \
  "$LOGBROOK_URL/indexes/checkout/logs" \
  -H "Authorization: Bearer $LOGBROOK_READ_TOKEN" \
  --data-urlencode "from=$FROM" --data-urlencode "to=$TO" \
  --data-urlencode 'limit=100' \
  --data-urlencode 'cursor=<next_cursor>'

What a cursor is bound to

Keep these the same on every page:

  • the index and the route style (/indexes/{index}/logs or the unprefixed /logs; cursors from the two are not interchangeable, even for default)
  • from and to
  • all filters and sources
  • the page size

A request that changes any of them returns 400. Listing the same sources or attribute conditions in a different order is fine.

A cursor is opaque and at most 1,024 bytes. Don’t build one from an event id, and don’t use a live-tail ID as one.

Consistency

  • Later pages return only events ingested before the first page, even if newer events fall inside the window. Start a new search to see those.
  • Retention can still remove events between pages, or expire the cursor itself, which returns 400.
  • Cursors stay valid across server restarts, but not after an index is deleted and created again.
  • A cursor grants no access. Every page is checked against the token that sends it.

Export every page as NDJSON

This loop writes every page to export.ndjson, replacing any existing file. If a request fails it exits nonzero and leaves a partial file, so delete that before you retry.

export EXPORT_INDEX=checkout
export TO=$(jq -nr 'now * 1000 | floor')
export FROM=$((TO - 3600000))

logbrook_page() {
  logbrook query "$EXPORT_INDEX" --from "$FROM" --to "$TO" --limit 500 --json "$@"
}

(
  set -eu
  cursor=''
  while :; do
    if [ -n "$cursor" ]; then
      page=$(logbrook_page --cursor "$cursor")
    else
      page=$(logbrook_page)
    fi
    printf '%s\n' "$page" | jq -c '.events[]'
    cursor=$(printf '%s\n' "$page" | jq -r '.next_cursor // empty')
    [ -n "$cursor" ] || break
  done
) > export.ndjson

wc -l < export.ndjson

EXPORT_INDEX, FROM and TO are plain shell variables, not settings the binary reads. Add filter options to logbrook_page, and keep the bounds fixed outside the loop.

Project to JSON or CSV

The server has no projection or CSV option. Reshape the export with jq:

# JSON projection; timestamps stay in milliseconds, IDs stay strings.
jq -c '{id, event_time, service, message, requestId: .attributes.requestId}' export.ndjson

# CSV with a header and proper quoting.
printf '%s\n' 'id,event_time,service,level,message'
jq -r '[.id, .event_time, .service, .level, .message] | @csv' export.ndjson

Filter an export client-side

What the server can’t filter, you can filter on the full export:

# Case-insensitive message match
jq -c 'select(.message | test("timeout"; "i"))' export.ndjson

# OR across two services
jq -c 'select(.service == "api" or .service == "worker")' export.ndjson

# Exclude health checks
jq -c 'select(.message | contains("/health") | not)' export.ndjson

# Numeric threshold on an attribute
jq -c 'select((.attributes.durationMs // 0) >= 1000)' export.ndjson

The filters above stream. jq -s loads the whole file into memory, so for large windows prefer server-side facets when the built-in fields are enough.

Next steps

Type to search…

↑↓ navigate↵ selectEsc close