console-log-cleaner — Claude Code hook: checks console.log
This hook looks for console. lines when editing JavaScript and TypeScript files on production branches. It can flag debug output before changes are merged.
npx claude-code-templates@latest --hook pre-tool/console-log-cleanerWhat console-log-cleaner does
Before an edit, the hook checks the current Git branch and the file extension. On main, master, production, and prod, it searches files ending in js, ts, jsx, tsx, mjs, or cjs for console. and prints a warning with matches.
The warning includes up to five matching lines, then reports how many more were found. It runs for the Edit tool and warns; it does not remove code. Its checks cover only the listed branches and extensions.
Install it with npx claude-code-templates@latest --hook pre-tool/console-log-cleaner. The Claude Code settings file is .claude/settings.json.
What it helps with
- find
console.calls on a production branch - check an edited JavaScript file for debug output
- spot matching lines in
tsandtsxfiles - see how many
console.matches remain before merging
Example prompts for the agent
- Edit
src/app.tsand show any warning aboutconsole.calls. - Change
src/main.jsxon the current branch and show matchingconsole.lines. - Edit
scripts/build.cjsand check the file forconsole..
How to install console-log-cleaner
1claude-code-templates
npx claude-code-templates@latest --hook pre-tool/console-log-cleaner2Manually: put the files in place
.claude/settings.jsonThe hooks block: event (PreToolUse, PostToolUse, Stop…) → command. Personal ones go to ~/.claude/settings.json
Step by step, with commands for each platform: Claude Code hooks: what they are, where to configure them, how to block a risky command
Questions about console-log-cleaner
How do I install console-log-cleaner in Claude Code?
Run npx claude-code-templates@latest --hook pre-tool/console-log-cleaner. The Claude Code settings file is .claude/settings.json.
What does console-log-cleaner do?
When an eligible file is edited on a production branch, the hook searches for console. and prints a warning with the matching lines.
Which files does console-log-cleaner check?
It checks files with the js, ts, jsx, tsx, mjs, and cjs extensions on main, master, production, and prod.
Source text
console-log-cleaner.json · MIT
{
"description": "Warns about console.log statements when editing files on production branches (main/master). Helps prevent debug code from reaching production.",
"hooks": {
"PreToolUse": [
{
"matcher": "Edit",
"hooks": [
{
"type": "command",
"command": "if git rev-parse --git-dir >/dev/null 2>&1 && [[ -n \"$CLAUDE_TOOL_FILE_PATH\" ]]; then BRANCH=$(git branch --show-current 2>/dev/null); if [[ \"$BRANCH\" =~ ^(main|master|production|prod)$ ]]; then EXT=\"${CLAUDE_TOOL_FILE_PATH##*.}\"; if [[ \"$EXT\" =~ ^(js|ts|jsx|tsx|mjs|cjs)$ ]]; then LOGS=$(grep -n 'console\\.' \"$CLAUDE_TOOL_FILE_PATH\" 2>/dev/null); if [[ -n \"$LOGS\" ]]; then echo \"⚠️ WARNING: console statements found on '$BRANCH' branch:\"; echo \"$LOGS\" | head -5; COUNT=$(echo \"$LOGS\" | wc -l); if [[ $COUNT -gt 5 ]]; then echo \"... and $((COUNT-5)) more\"; fi; echo \"Consider removing debug statements before merging.\"; fi; fi; fi; fi"
}
]
}
]
}
}
© davila7 · MIT · davila7/claude-code-templates