A powerful Python utility that provides comprehensive log management capabilities. This dual-purpose tool helps you maintain clean production code by automatically removing logging statements and console outputs from your codebase, while also efficiently managing your log files through automated cleaning and scheduling. It intelligently handles Python logging statements and JavaScript/TypeScript console outputs, supporting both single-line and multi-line statements, alongside sophisticated log file management features for automated retention and cleanup.
-
Multi-Language Support
- Python (
.py
) - JavaScript (
.js
) - TypeScript (
.ts
) - React files (
.jsx
,.tsx
)
- Python (
-
Smart Code Detection
- Identifies and removes Python logging statements (
logger.*
,logging.*
) - Catches all console methods in JavaScript/TypeScript
- Handles multi-line logging statements
- Preserves code formatting
- Identifies and removes Python logging statements (
-
Intelligent Log Detection
- Automatically identifies log files by extension and content
- Supports multiple log file formats and patterns
- Handles various timestamp formats
- Works with rotated log files (e.g., .log.1, .log.2)
-
Automated Cleaning
- Remove log entries older than specified date
- Scheduled cleaning through cron jobs
- Configurable retention periods
- Preserves file structure and empty lines
-
Safety Features
- Automatic backup creation
- Detailed logging of all changes
- Preview of affected files before cleanup
- Undo capability through backups
-
User Interface
- Interactive command-line interface
- Clear progress indicators
- Colored output for better visibility
- Comprehensive summary reports
You can install the package directly from GitHub:
# Install latest version
pip install git+https://github.com/cypher-o/log_cleaner.git
# Or specify a version
pip install git+https://github.com/cypher-o/log_cleaner.git@v1.0.0
-
Clone this repository:
git clone https://github.com/cypher-o/log_cleaner.git cd log_cleaner
-
Install in development mode:
pip install -e .
# Start the interactive tool
log-cleaner
# Or using Python's -m flag
python -m log_cleaner
Clean logging statements from a specific directory:
python log_cleaner.py --clean-code ./my_project
Clean specific files:
python log_cleaner.py --clean-code ./src/main.py ./src/utils.js
Clean old log entries:
# Clean logs older than 30 days
python log_cleaner.py --clean-logs ./logs --days 30
# Clean logs before specific date
python log_cleaner.py --clean-logs ./logs --before 2024-01-01
Set up automated cleaning:
# Schedule daily cleanup at 2:30 AM
python log_cleaner.py --schedule ./logs --hour 2 --minute 30
# Remove scheduled cleanup
python log_cleaner.py --remove-schedule
The tool supports various log formats including:
- ISO format timestamps:
2024-02-15 10:30:45 INFO Started application
- Unix timestamps:
1644915045 Started backup process
- Custom formats:
[Feb 15 10:30:45 2024] Server started
# Import statements
import logging
from logging import getLogger
# Logger initialization
logger = logging.getLogger(__name__)
_logger = logging.getLogger()
# Logging statements
logger.info("Message")
logger.error(f"Error: {error}")
_logger.warning(
f"Warning message with "
"multiple lines"
)
// Console statements
console.log("Message");
console.error("Error");
console.warn(
"Multi-line " +
"warning message"
);
- Backups are stored in a
backup
directory with timestamps - Each backup includes:
- Original files with directory structure preserved
- Detailed log file of all changes
- Timestamp-based organization for easy recovery
Clean a specific directory:
python log_cleaner.py
# Select "Select a directory"
# Enter: ./my_project
python log_cleaner.py
# Select "Select specific files"
# Enter files one by one:
# ./src/main.py
# ./src/utils.js
- Always run on version-controlled code
- Review changes before confirming
- Enable backups for important files
- Test after cleaning
- Schedule automated cleaning during low-traffic periods
- Monitor disk space usage
- Set appropriate retention periods
- Complex string concatenations may require manual review
- Cannot detect logging statements in string literals or comments
- May require adjustment of patterns for custom log formats
- Scheduled cleaning requires cron daemon to be running
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by the need for clean production code
- Built with best practices from Python and JavaScript communities
For issues, questions, or contributions, please:
- Open an issue in the GitHub repository
- Include sample code or log files that demonstrate the problem
- Provide the error message if applicable
- Describe the expected behavior
- Added log file management capabilities
- Automated cleaning with cron integration
- Support for multiple log formats
- Improved log file detection
- Initial release
- Multi-language code cleanup
- Backup functionality
- Interactive CLI
Made with Python by Olumide Awodeji