Skip to main content

A tool to clean logging statements from code

Project description

Log Cleaner Pro

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.

Features

Code Cleanup

  • Multi-Language Support

    • Python (.py)
    • JavaScript (.js)
    • TypeScript (.ts)
    • React files (.jsx, .tsx)
  • 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

Log File Management

  • 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

Installation

Using pip (Recommended)

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

From source

  1. Clone this repository:

    git clone https://github.com/cypher-o/log_cleaner.git
    cd log_cleaner
    
  2. Install in development mode:

    pip install -e .
    

Usage

Basic Command Line Usage

# Start the interactive tool
log-cleaner

# Or using Python's -m flag
python -m log_cleaner

Code Cleanup Examples

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

Log File Management Examples

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

Supported Log Formats

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

Supported Code Patterns

Python

# 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"
)

JavaScript/TypeScript

// Console statements
console.log("Message");
console.error("Error");
console.warn(
    "Multi-line " +
    "warning message"
);

Backup and Recovery

  • 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

Examples

Basic Usage

Clean a specific directory:

python log_cleaner.py
# Select "Select a directory"
# Enter: ./my_project

Clean Specific Files

python log_cleaner.py
# Select "Select specific files"
# Enter files one by one:
# ./src/main.py
# ./src/utils.js

Best Practices

  1. Always run on version-controlled code
  2. Review changes before confirming
  3. Enable backups for important files
  4. Test after cleaning
  5. Schedule automated cleaning during low-traffic periods
  6. Monitor disk space usage
  7. Set appropriate retention periods

Limitations

  • 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

Contributing

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.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Inspired by the need for clean production code
  • Built with best practices from Python and JavaScript communities

Support

For issues, questions, or contributions, please:

  1. Open an issue in the GitHub repository
  2. Include sample code or log files that demonstrate the problem
  3. Provide the error message if applicable
  4. Describe the expected behavior

Changelog

v1.1.0 (2025-02-15)

  • Added log file management capabilities
  • Automated cleaning with cron integration
  • Support for multiple log formats
  • Improved log file detection

v1.0.0 (2025-02-09)

  • Initial release
  • Multi-language code cleanup
  • Backup functionality
  • Interactive CLI

Made with Python by Olumide Awodeji

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

log_cleaner-1.1.0.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

log_cleaner-1.1.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file log_cleaner-1.1.0.tar.gz.

File metadata

  • Download URL: log_cleaner-1.1.0.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for log_cleaner-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a7c5cf084f452a76bee2fd629c6a87c7317273d57d24b18dee9fa41c1702a385
MD5 ae8ed7d17e1d5f37cabd956ad5dba3ba
BLAKE2b-256 4eeb5e9f6a95112cb94bdeb36d2d772e9c88befacd9503d641a55135ea3b639f

See more details on using hashes here.

File details

Details for the file log_cleaner-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: log_cleaner-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for log_cleaner-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d92840ab5c6db63121540d191cf8d4eeb3a03f5199cba8292e46fed24deca521
MD5 207c7ee4c3555bf8bf33504381294883
BLAKE2b-256 bd7b7df49e1d51d12e6228256a54f308960bae392a886f93961c3e6fbbda63a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page