Export and pipe
Export the filtered entries of your last tail to a file, or pipe them to another command.
Export to a file
Section titled “Export to a file”Basic export
Section titled “Basic export”pgtail> export /tmp/errors.logExported 42 entries to /tmp/errors.logExports the entries of the last tail that match the current filters. If the file exists, pgtail asks before
replacing it: File /tmp/errors.log exists. Overwrite? [y/N].
Formats
Section titled “Formats”export --format text /tmp/logs.txt # The log lines as PostgreSQL wrote them (default)export --format json /tmp/logs.json # JSON Linesexport --format csv /tmp/logs.csv # CSV with a header rowJSON Lines writes one object per entry, and CSV one row per entry with the same fields:
{"timestamp": "2024-01-15T10:30:45.123000+00:00", "level": "ERROR", "pid": 12345, "message": "relation \"users\" does not exist"}timestamp,level,pid,messageKeeping colors
Section titled “Keeping colors”export --highlighted /tmp/colored.logWrites each text entry as pgtail displays it, with its colors as ANSI escape sequences, for viewing with less -R
or cat.
Append mode
Section titled “Append mode”export --append /tmp/logs.txtAdds to the end of an existing file instead of replacing it.
Time-scoped export
Section titled “Time-scoped export”export --since 1h /tmp/recent.logExports only entries from the last hour. --since takes any time format.
Continuous export
Section titled “Continuous export”export --follow /tmp/live.logKeeps writing new entries to the file as they arrive, like tail -f | tee, and shows them on screen as well. Press
Ctrl+C to stop. --follow cannot be combined with --append or --since.
Pipe to commands
Section titled “Pipe to commands”Basic pipe
Section titled “Basic pipe”pgtail> pipe wc -lSends the filtered entries to a command’s standard input and prints what it writes. The command runs in your shell
(sh -c, or cmd /c on Windows), so pipes and quoting work as they would at a prompt.
With a format
Section titled “With a format”pipe --format json jq -r '.level'Examples
Section titled “Examples”pipe grep ERROR # Search within entriespipe wc -l # Count entriespipe sort | uniq -c # Count identical linespipe --format json jq -r '.level' | sort | uniq -c # Count entries by levelThe command’s error output is printed after stderr:, and a non-zero exit code is reported.
Export in tail mode
Section titled “Export in tail mode”Tail mode has its own export, which writes the entries the log is showing:
tail> export /tmp/logs.txttail> export /tmp/logs.json --format jsontail> export /tmp/logs.txt --highlightedThe entry buffer
Section titled “The entry buffer”export and pipe in the REPL work on the entries of the most recent tail:
- The last 10,000 entries are kept
- Filters apply when you export, so you can change them and export again
- Both full screen tail mode and streaming fill the buffer, and it stays after you leave tail mode
pgtail> tail 0# ... read the log, leave with 'q' ...pgtail> export /tmp/session.logIf nothing has been tailed yet, export and pipe say No log file loaded. Use 'tail <instance>' first.