Structured logging to TSV files with JSON context: grep-able, compressible and searchable.

0 downloads

dbbasic-logs/scripts/view-logs.sh
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-logs.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-logs.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-logs.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add PyPI badges and links to README cea7e20 1 year, 1 month ago History
view-logs.sh · 6.1 KB · 208 lines
1#!/bin/bash
2#
3# Utility script for viewing and searching logs
4#
5# Usage:
6# ./view-logs.sh tail [app|errors|access] [lines]
7# ./view-logs.sh search "pattern" [log_type] [days]
8# ./view-logs.sh today [app|errors|access]
9# ./view-logs.sh errors [days]
10#
11
12set -e
13
14LOG_DIR="${LOG_DIR:-data/logs}"
15
16# Colors for output
17RED='\033[0;31m'
18YELLOW='\033[1;33m'
19GREEN='\033[0;32m'
20BLUE='\033[0;34m'
21NC='\033[0m' # No Color
22
23usage() {
24 echo "DBBasic Logs Viewer"
25 echo ""
26 echo "Usage:"
27 echo " $0 tail [log_type] [lines] - View recent logs"
28 echo " $0 search <pattern> [type] [days] - Search logs"
29 echo " $0 today [log_type] - View today's logs"
30 echo " $0 errors [days] - View recent errors"
31 echo " $0 stats - Show log statistics"
32 echo ""
33 echo "Log types: app, errors, access, all"
34 echo ""
35 echo "Examples:"
36 echo " $0 tail app 100 - Last 100 app logs"
37 echo " $0 search ERROR app 7 - Search for errors in last 7 days"
38 echo " $0 today errors - View today's error logs"
39 echo " $0 errors 1 - View last day of errors"
40 exit 1
41}
42
43# Tail logs
44tail_logs() {
45 local log_type=${1:-app}
46 local lines=${2:-100}
47 local today=$(date +%Y-%m-%d)
48 local log_file="${LOG_DIR}/${log_type}/${today}.tsv"
49
50 if [ ! -f "$log_file" ]; then
51 echo -e "${YELLOW}No logs found for today: ${log_type}${NC}"
52 exit 1
53 fi
54
55 echo -e "${BLUE}=== Last ${lines} lines from ${log_type} ===${NC}"
56 tail -n "$lines" "$log_file" | while IFS=$'\t' read -r timestamp level message context; do
57 local date_str=$(date -d "@${timestamp}" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -r "${timestamp}" "+%Y-%m-%d %H:%M:%S" 2>/dev/null)
58
59 case "$level" in
60 ERROR)
61 echo -e "${RED}[${date_str}] ${level} ${message}${NC}"
62 ;;
63 WARNING)
64 echo -e "${YELLOW}[${date_str}] ${level} ${message}${NC}"
65 ;;
66 *)
67 echo -e "${GREEN}[${date_str}]${NC} ${level} ${message}"
68 ;;
69 esac
70
71 if [ "$context" != "{}" ]; then
72 echo " Context: $context"
73 fi
74 done
75}
76
77# Search logs
78search_logs() {
79 local pattern=$1
80 local log_type=${2:-app}
81 local days=${3:-7}
82
83 echo -e "${BLUE}=== Searching for '${pattern}' in ${log_type} (last ${days} days) ===${NC}"
84
85 for ((i=0; i<days; i++)); do
86 local date=$(date -d "${i} days ago" +%Y-%m-%d 2>/dev/null || date -v-${i}d +%Y-%m-%d 2>/dev/null)
87
88 if [ "$log_type" = "all" ]; then
89 types="app errors access"
90 else
91 types="$log_type"
92 fi
93
94 for type in $types; do
95 local log_file="${LOG_DIR}/${type}/${date}.tsv"
96 local log_file_gz="${log_file}.gz"
97
98 if [ -f "$log_file" ]; then
99 grep -i "$pattern" "$log_file" 2>/dev/null | while IFS=$'\t' read -r timestamp level message context; do
100 local date_str=$(date -d "@${timestamp}" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -r "${timestamp}" "+%Y-%m-%d %H:%M:%S" 2>/dev/null)
101 echo -e "${GREEN}[${type}]${NC} [${date_str}] ${level} ${message}"
102 if [ "$context" != "{}" ]; then
103 echo " $context"
104 fi
105 done
106 elif [ -f "$log_file_gz" ]; then
107 zgrep -i "$pattern" "$log_file_gz" 2>/dev/null | while IFS=$'\t' read -r timestamp level message context; do
108 local date_str=$(date -d "@${timestamp}" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -r "${timestamp}" "+%Y-%m-%d %H:%M:%S" 2>/dev/null)
109 echo -e "${GREEN}[${type}]${NC} [${date_str}] ${level} ${message}"
110 if [ "$context" != "{}" ]; then
111 echo " $context"
112 fi
113 done
114 fi
115 done
116 done
117}
118
119# View today's logs
120today_logs() {
121 local log_type=${1:-app}
122 local today=$(date +%Y-%m-%d)
123 local log_file="${LOG_DIR}/${log_type}/${today}.tsv"
124
125 echo -e "${BLUE}=== Today's ${log_type} logs ===${NC}"
126
127 if [ ! -f "$log_file" ]; then
128 echo -e "${YELLOW}No logs found for today${NC}"
129 exit 1
130 fi
131
132 cat "$log_file" | while IFS=$'\t' read -r timestamp level message context; do
133 local date_str=$(date -d "@${timestamp}" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -r "${timestamp}" "+%Y-%m-%d %H:%M:%S" 2>/dev/null)
134
135 case "$level" in
136 ERROR)
137 echo -e "${RED}[${date_str}] ${level} ${message}${NC}"
138 ;;
139 WARNING)
140 echo -e "${YELLOW}[${date_str}] ${level} ${message}${NC}"
141 ;;
142 *)
143 echo -e "[${date_str}] ${level} ${message}"
144 ;;
145 esac
146
147 if [ "$context" != "{}" ]; then
148 echo " Context: $context"
149 fi
150 done
151}
152
153# View recent errors
154view_errors() {
155 local days=${1:-1}
156 search_logs "ERROR" "all" "$days"
157}
158
159# Show statistics
160show_stats() {
161 echo -e "${BLUE}=== Log Statistics ===${NC}"
162 echo ""
163
164 for log_type in app errors access; do
165 local dir="${LOG_DIR}/${log_type}"
166 if [ -d "$dir" ]; then
167 local file_count=$(find "$dir" -type f | wc -l)
168 local size=$(du -sh "$dir" | cut -f1)
169 echo -e "${GREEN}${log_type}:${NC}"
170 echo " Files: $file_count"
171 echo " Size: $size"
172
173 # Today's stats
174 local today=$(date +%Y-%m-%d)
175 local log_file="${dir}/${today}.tsv"
176 if [ -f "$log_file" ]; then
177 local lines=$(wc -l < "$log_file")
178 echo " Today's entries: $lines"
179 fi
180 echo ""
181 fi
182 done
183}
184
185# Main command dispatcher
186case "${1:-}" in
187 tail)
188 tail_logs "$2" "$3"
189 ;;
190 search)
191 if [ -z "$2" ]; then
192 usage
193 fi
194 search_logs "$2" "$3" "$4"
195 ;;
196 today)
197 today_logs "$2"
198 ;;
199 errors)
200 view_errors "$2"
201 ;;
202 stats)
203 show_stats
204 ;;
205 *)
206 usage
207 ;;
208esac