-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog-tool-usage.sh
More file actions
executable file
·34 lines (28 loc) · 1.03 KB
/
log-tool-usage.sh
File metadata and controls
executable file
·34 lines (28 loc) · 1.03 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
#!/bin/bash
# log-tool-usage.sh
# PostToolUse hook (no matcher - fires for every tool).
# Appends a JSON line to ~/.claude/tool-usage.log containing:
# - timestamp (ISO 8601)
# - session_id
# - tool_name
# - file_path (if present, for Edit/Write/Read tools)
# - command (if present, for the Bash tool)
# Non-blocking: always exits 0.
set -euo pipefail
LOG_DIR="$HOME/.claude"
LOG_FILE="$LOG_DIR/tool-usage.log"
# Ensure the log directory exists.
mkdir -p "$LOG_DIR"
INPUT=$(cat)
# Build a JSON log entry using jq. Omit null fields gracefully.
jq -c '{
timestamp: (now | todate),
session_id: (.session_id // null),
tool_name: (.tool_name // null),
file_path: (.tool_input.file_path // null),
command: (.tool_input.command // null)
} | with_entries(select(.value != null))' <<< "$INPUT" >> "$LOG_FILE" 2>/dev/null || {
# If jq fails (e.g., malformed input), write a minimal fallback entry.
echo "{\"timestamp\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\",\"error\":\"failed to parse hook input\"}" >> "$LOG_FILE" || true
}
exit 0