Production-grade interactive CLI tool to scan and selectively remove console statements using AST parsing.
Safely handles all edge cases that regex-based tools miss: strings containing parentheses, template literals, nested expressions, regex patterns, and more.
- π AST-Based Scanning - Uses Babel parser to understand JavaScript syntax perfectly
- π― Selective Removal - Choose which console methods to remove (log, warn, info, debug, error, etc.)
- π Detailed Reports - See exactly where console statements are, by file and by method
- π Git-Aware Filtering - Filter by author or line-level uncommitted changes (team-safe!)
β οΈ Side-Effect Detection - Warns about console statements with side effects (++, --, await, assignments)- π‘οΈ Scope-Aware - Never removes shadowed console objects (custom loggers, mocks)
- πΎ Automatic Backups - Creates compressed backups before making changes
- π Easy Restore - Restore from backup if something goes wrong
- β‘ Dry Run Mode - Preview changes without modifying files
- π Production-Safe - Handles all edge cases that break regex-based tools
npm install -g log-sweepThen use anywhere:
log-sweep scan
log-sweep remove # you can use 'logsweep' as a short aliasnpm install --save-dev log-sweepThen use via npx:
npx log-sweep scanOr add to package.json scripts:
{
"scripts": {
"console:scan": "log-sweep scan src",
"console:clean": "log-sweep remove src"
}
}# Scan current directory
log-sweep scan
# Scan specific directory
log-sweep scan ./src
# Save results to JSON
log-sweep scan --output results.json
# Scan only YOUR console statements (git blame)
log-sweep scan --git-mine
# Scan only uncommitted changes
log-sweep scan --git-uncommitted
# Combine filters: only YOUR uncommitted console statements
log-sweep scan --git-mine --git-uncommitted# Interactive removal with backup
log-sweep remove
# Remove from specific directory
log-sweep remove ./src
# Dry run (preview only)
log-sweep remove --dry-run
# Skip backup (not recommended)
log-sweep remove --no-backup
# Remove only YOUR console statements (safe for teams!)
log-sweep remove --git-mine
# Remove only from uncommitted changes
log-sweep remove --git-uncommitted
# Combine: remove only YOUR uncommitted console statements
log-sweep remove --git-mine --git-uncommittedlog-sweep restore /tmp/log-sweep-backup-2024-01-15.tar.gzScan directory for console statements and display detailed report.
Options:
-e, --exclude <patterns...>- Exclude directories (default: node_modules, .git, dist, build)-o, --output <file>- Save results to JSON file--git-mine- Only show console statements authored by you (uses git blame)--git-uncommitted- Only show console statements in uncommitted changes
Examples:
# Basic scan
log-sweep scan
# Scan with exclusions
log-sweep scan --exclude test coverage docs
# Save report
log-sweep scan --output console-report.json
# Team-safe: scan only YOUR console statements
log-sweep scan --git-mine
# Pre-commit: scan uncommitted changes
log-sweep scan --git-uncommittedInteractively remove console statements with AST-based safety.
Options:
-e, --exclude <patterns...>- Exclude directories--no-backup- Skip creating backup--dry-run- Preview changes without applying--git-mine- Only remove console statements authored by you (uses git blame)--git-uncommitted- Only remove console statements in uncommitted changes
Examples:
# Interactive removal (recommended)
log-sweep remove
# Dry run to preview
log-sweep remove --dry-run
# Remove without backup (use with caution)
log-sweep remove --no-backup
# Team-safe: remove only YOUR console statements
log-sweep remove --git-mine
# Pre-commit: remove from uncommitted changes only
log-sweep remove --git-uncommitted
# Safest: remove only YOUR uncommitted statements
log-sweep remove --git-mine --git-uncommittedInteractive Flow:
- Scans your codebase
- Shows summary of console statements by type
- Warns if side effects detected (e.g.,
console.log(counter++)) - Lets you select which methods to remove (checkboxes)
- Asks how to handle side effects (skip or remove)
- Shows preview of what will be changed
- Asks for confirmation
- Creates backup (default)
- Removes selected console statements (respecting side-effect choices)
- Displays summary and backup location
Restore files from a backup archive.
Example:
log-sweep restore /tmp/log-sweep-backup-2024-01-15.tar.gzWorking on a team? log-sweep has you covered with git-aware filtering:
Use git blame to only scan/remove console statements you wrote:
# See only YOUR console statements
log-sweep scan --git-mine
# Remove only YOUR console statements (safe for shared codebases!)
log-sweep remove --git-minePerfect for:
- Cleaning up your own debug logs without touching teammates' code
- Team codebases where console.log might be intentional logging
- Code reviews - clean up before committing
Only scan/remove console statements in uncommitted lines (not entire files):
# See console statements in uncommitted lines only
log-sweep scan --git-uncommitted
# Pre-commit cleanup - remove from uncommitted lines only
log-sweep remove --git-uncommittedHow it works:
- Uses
git diffto identify exact changed lines - Only touches console statements on those specific lines
- Safe: won't remove teammates' committed logs in the same file
Perfect for:
- Pre-commit hooks
- Quick cleanup before committing
- Avoiding changes to committed/pushed code
# Only YOUR console statements in YOUR uncommitted changes
log-sweep remove --git-mine --git-uncommittedThis ensures you never accidentally remove a teammate's console statement.
Regex-based tools break on these edge cases:
// β Regex breaks: parentheses in strings
console.log("This has ) parens ( in it", data);
// β Regex breaks: template literals
console.log(`Value: ${func(a, b)} and more`, obj);
// β Regex breaks: nested expressions
console.log("Result:", array.filter(x => (x > 0)).map(y => ({id: y})));
// β Regex breaks: regex patterns
console.log("Test:", /\(.*\)/.test(str));
// β Regex breaks: optional chaining
console?.log("test");
// β Regex breaks: computed properties
console['log']("test");
// β Regex can't detect: shadowed console (would incorrectly remove!)
const console = myLogger;
console.log("This is NOT the global console!");log-sweep handles ALL of these correctly because it understands JavaScript syntax using Babel's AST parser with full scope analysis.
π Console Statement Scanner
β Scan complete!
π Scan Results:
Total console statements: 287
Files with console statements: 45
π By Method:
log : 186 occurrences
error : 54 occurrences
warn : 32 occurrences
info : 12 occurrences
debug : 3 occurrences
π Top Files:
1. src/utils/logger.js (34)
2. src/services/api.js (28)
3. src/components/App.jsx (19)
... and 42 more files
π§Ή Console Statement Remover
β Scan complete!
β οΈ Warning: Potential Side Effects Detected
Found 2 console statements with potential side effects:
(e.g., ++, --, assignments, await, yield)
β’ src/utils.js:45
console.log(counter++)
β’ src/api.js:123
console.log(result = await fetchData())
π Select console methods to remove:
? Which console methods should be removed?
β log - 186 occurrences (β οΈ 2 with side effects)
β― error - 54 occurrences
β warn - 32 occurrences
β info - 12 occurrences
β debug - 3 occurrences
? How should statements with side effects be handled?
β Skip them (safer - keep statements with side effects)
β Remove them anyway (risky - may break code logic)
? Would you like to preview changes before applying? (Y/n)
π Preview of changes:
Files to modify: 43
Statements to remove: 231
Statements to skip (side effects): 2
Methods: log, warn, info, debug
β’ src/utils/logger.js (28 statements)
β’ src/services/api.js (24 statements)
... and 41 more files
β οΈ This will modify your files. Continue? (y/N)
β Backup created: /tmp/log-sweep-backup-2024-01-15.tar.gz
β Successfully removed 231 console statements!
π‘ To restore, run: log-sweep restore /tmp/log-sweep-backup-2024-01-15.tar.gz
- AST Parsing - Understands JavaScript syntax perfectly
- Scope Analysis - Never removes shadowed console objects (custom loggers, mocks)
- Side-Effect Detection - Warns about
console.log(counter++),await, assignments - Line-Level Git Filtering - Only touches specific uncommitted lines, not entire files
- Git-Aware Filtering - Filter by author or uncommitted changes (team-safe!)
- Automatic Backups - Compressed tar.gz backups before changes
- Dry Run Mode - Preview without modifying
- Confirmation Prompts - Never surprises you
- Error Recovery - Auto-restores backup on failure
- Selective Removal - Only removes what you choose
- Handles Edge Cases - Strings, regex, templates, optional chaining, computed properties
All standard console methods are supported:
Common:
console.logconsole.warnconsole.infoconsole.debugconsole.error
Debugging:
console.traceconsole.tableconsole.dirconsole.dirxmlconsole.assert
Counting & Timing:
console.countconsole.countResetconsole.timeconsole.timeEndconsole.timeLogconsole.timeStamp
Grouping:
console.groupconsole.groupCollapsedconsole.groupEnd
Profiling:
console.profileconsole.profileEndconsole.clear
- JavaScript (
.js,.mjs,.cjs) - JSX (
.jsx) - TypeScript (
.ts) - TSX (
.tsx)
By default, these directories are excluded:
node_modules/.git/dist/build/*.min.jsfiles
Add custom exclusions:
log-sweep scan --exclude test coverage docs __tests__DEBUG=1- Show detailed error messages and stack traces
DEBUG=1 log-sweep scan{
"scripts": {
"prebuild": "log-sweep remove src --no-backup",
"analyze": "log-sweep scan src --output console-report.json"
}
}Fail build if console statements are found:
#!/bin/bash
log-sweep scan src --output report.json
count=$(node -e "console.log(require('./report.json').totalCount)")
if [ "$count" -gt 0 ]; then
echo "β Found $count console statements!"
exit 1
fiMIT
If you get parse errors on specific files:
# Exclude problematic files
log-sweep scan --exclude problematic-folder
# Enable debug mode
DEBUG=1 log-sweep scanIf something goes wrong:
# Find your backup
ls -lt /tmp/log-sweep-backup-*.tar.gz | head -1
# Restore
log-sweep restore /tmp/log-sweep-backup-XXXXX.tar.gzconst { scanDirectory } = require('log-sweep/src/scanner');
const { removeConsoleStatements } = require('log-sweep/src/remover');
// Scan
const results = await scanDirectory('./src', ['test', 'coverage']);
console.log(`Found ${results.totalCount} console statements`);
// Remove
const removed = await removeConsoleStatements(
['./src/file1.js', './src/file2.js'],
['log', 'warn'],
false // dryRun
);
console.log(`Removed ${removed} statements`);- β
Always use dry run first -
--dry-runto preview - β Keep backups enabled - They're automatic and compressed
- β Keep console.error - Uncheck 'error' in interactive mode
- β Review git diff - Always check changes before committing
- β Run in CI/CD - Prevent console statements from reaching production
**Made with β€οΈ by AmElmo