107 lines
2.0 KiB
Bash
Executable File
107 lines
2.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
function print_help_and_exit() {
|
|
cat <<EOF
|
|
View structured logs
|
|
Usage: slog.sh [OPTIONS] [POSITIONALS]
|
|
|
|
Positionals:
|
|
LOG_FILE The log file to read
|
|
|
|
Options:
|
|
-t,--type The log type to view (e.g., book, trade_rec, execution_report security, etc.)
|
|
-n,--lines The number of lines to tail (counting backwards from EOF)
|
|
-l,--list-types List structured log types
|
|
-f,--file The log file to read (defaults to ~/sparkdata/data/logging.sparkd/log)
|
|
-h,--help Print this help message and exit
|
|
|
|
EOF
|
|
exit $1
|
|
}
|
|
|
|
|
|
lines=1000
|
|
type=".*"
|
|
follow=0
|
|
list_types=0
|
|
show_headers=1
|
|
log_file=""
|
|
while [ $# -gt 0 ]; do
|
|
case "$1" in
|
|
-t)
|
|
;&
|
|
--type)
|
|
type=$2
|
|
shift 2
|
|
;;
|
|
-f)
|
|
;&
|
|
--follow)
|
|
follow=1
|
|
shift 1
|
|
;;
|
|
-n)
|
|
;&
|
|
--lines)
|
|
lines=$2
|
|
shift 2
|
|
;;
|
|
-H)
|
|
;&
|
|
--headers)
|
|
show_headers=1
|
|
shift 1
|
|
;;
|
|
-noH)
|
|
;&
|
|
--no-headers)
|
|
show_headers=0
|
|
shift 1
|
|
;;
|
|
-l)
|
|
;&
|
|
--list-types)
|
|
list_types=1
|
|
shift 1
|
|
;;
|
|
-f)
|
|
;&
|
|
--file)
|
|
log_file="$2"
|
|
shift 2
|
|
;;
|
|
-h)
|
|
;&
|
|
--help)
|
|
print_help_and_exit 1
|
|
;;
|
|
*)
|
|
if [ -n "$log_file" ]; then
|
|
echo -e "Invalid input: log file specified twice.\n"
|
|
print_help_and_exit 1
|
|
fi
|
|
log_file="$1"
|
|
shift 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [ -z "$log_file" ]; then
|
|
log_file=$HOME/sparkdata/data/logging.sparkd/log
|
|
fi
|
|
|
|
if [ $list_types -eq 1 ]; then
|
|
types="$(grep -e "\[.*.json\]" "$log_file" | cut -d'|' -f 10 | cut -d']' -f 1 | cut -d'[' -f 2 | cut -d'.' -f 1)"
|
|
echo "$types" | sort | uniq
|
|
exit
|
|
fi
|
|
tail_args=(-n $lines $log_file)
|
|
if [ $follow -eq 1 ]; then
|
|
tail_args=(-f ${tail_args[@]})
|
|
fi
|
|
if [ $show_headers -eq 1 ]; then
|
|
tail ${tail_args[@]} | grep -e "\[${type}.json\]" | cut -d'|' -f 10
|
|
else
|
|
tail ${tail_args[@]} | grep -e "\[${type}.json\]" | cut -d'|' -f 10 | cut -d']' -f 2-
|
|
fi
|