Auto-logging every file Claude edits with a PostToolUse hook, tested live

Instead of asking every time, wire a hook that fires automatically — built & verified live.

Stop asking "format this" every time

A `PostToolUse` hook runs a fixed shell command right after a tool call (like Edit/Write) succeeds — no need to ask in plain language each time.

PostToolUse vs the other hook events

Unlike `PreToolUse` (blocks before execution) or `SessionStart`/`SubagentStop` (session lifecycle, covered in other articles), `PostToolUse` fires after a tool succeeds.

The minimal setup we built and tested

A `PostToolUse` hook matching `Edit|Write`, whose script extracts the edited file path from stdin JSON and appends it to a log:

// .claude/settings.json
{ "hooks": { "PostToolUse": [ { "matcher": "Edit|Write",
  "hooks": [{ "type": "command", "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/format-log.sh" }] } ] } }

The real result

Asking Claude to create `hello.txt` triggered the hook, and `.claude/format.log` really did gain this line:

[2026-07-29 06:27:16] formatted: <project>/hello.txt

Swapping in a real formatter (untested extension)

Replacing the log line with `npx prettier --write "$file"` should work — the file-path extraction is verified, but we didn't test the prettier call itself. Verify in your own setup first.

Scoping to specific extensions

Add a branch checking the file's extension inside the same tested script to narrow what gets processed.

FAQ

When does a PostToolUse hook fire?
Verified live — right after a tool call like Edit/Write succeeds.
What data reaches the hook script?
JSON on stdin, verified to include tool_input.file_path.
Can I swap in a real formatter like prettier?
The skeleton should support it, but we only tested the logging version — verify the swap yourself.
Can I limit it to certain file types?
Yes, add a branch checking the file extension in the script.
Can it report failures back to the AI?
The exit-code-2 pattern (verified in our hooks-blocking article) can be adapted, though that specific adaptation is untested here.