-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-logs.sh
More file actions
69 lines (62 loc) · 2.72 KB
/
format-logs.sh
File metadata and controls
69 lines (62 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# Formats Claude Code streaming JSON output into readable logs
while IFS= read -r line; do
# Skip empty lines
[ -z "$line" ] && continue
# Try to parse as JSON
type=$(echo "$line" | grep -o '"type":"[^"]*"' | head -1 | cut -d'"' -f4)
case "$type" in
"system")
subtype=$(echo "$line" | grep -o '"subtype":"[^"]*"' | cut -d'"' -f4)
if [ "$subtype" = "init" ]; then
model=$(echo "$line" | grep -o '"model":"[^"]*"' | cut -d'"' -f4)
echo "🚀 Session started (model: $model)"
fi
;;
"assistant")
# Extract text content
text=$(echo "$line" | grep -o '"text":"[^"]*"' | head -1 | cut -d'"' -f4)
if [ -n "$text" ] && [ "$text" != "null" ]; then
echo "💭 $text"
fi
# Check for tool use
tool=$(echo "$line" | grep -o '"name":"[^"]*"' | head -1 | cut -d'"' -f4)
if [ -n "$tool" ] && [ "$tool" != "null" ]; then
# Get description or command if available
desc=$(echo "$line" | grep -o '"description":"[^"]*"' | cut -d'"' -f4)
cmd=$(echo "$line" | grep -o '"command":"[^"]*"' | cut -d'"' -f4)
file=$(echo "$line" | grep -o '"file_path":"[^"]*"' | cut -d'"' -f4)
if [ -n "$desc" ]; then
echo "🔧 [$tool] $desc"
elif [ -n "$cmd" ]; then
echo "🔧 [$tool] $cmd"
elif [ -n "$file" ]; then
echo "🔧 [$tool] $file"
else
echo "🔧 [$tool]"
fi
fi
;;
"user")
# Tool result - only show errors
is_error=$(echo "$line" | grep -o '"is_error":true')
if [ -n "$is_error" ]; then
content=$(echo "$line" | grep -o '"content":"[^"]*"' | head -1 | cut -d'"' -f4)
echo "❌ Error: $content"
fi
;;
"result")
subtype=$(echo "$line" | grep -o '"subtype":"[^"]*"' | cut -d'"' -f4)
cost=$(echo "$line" | grep -o '"total_cost_usd":[0-9.]*' | cut -d':' -f2)
duration=$(echo "$line" | grep -o '"duration_ms":[0-9]*' | cut -d':' -f2)
if [ "$subtype" = "success" ]; then
echo ""
echo "✅ Session completed successfully"
[ -n "$duration" ] && echo "⏱️ Duration: $((duration / 1000))s"
[ -n "$cost" ] && echo "💰 Cost: \$${cost}"
elif [ "$subtype" = "error" ]; then
echo "❌ Session failed"
fi
;;
esac
done